Silverlight的DataGrid合并单元格

时间:2023-12-06 08:50:32

现在也不知道还有没有同学做Silverlight开发了,我是一个Silverlight菜鸟,遇到问题也很难百度查到。就简单的记录一下这两天遇到的问题,并做了一个简单的小Demo,希望能够帮助到其他同学,并为自己翻看。

DataGrid合并单元格,实现结果如下:

Silverlight的DataGrid合并单元格

Silverlight后台代码如下:

 /// <summary>
/// 学生实体类
/// </summary>
public class Students
{
private string name;
/// <summary>
/// 学生姓名
/// </summary>
public string Name
{
get { return name; }
set { name = value; }
} private int age;
/// <summary>
/// 学生年龄
/// </summary>
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
/// <summary>
/// 学生性别
/// </summary>
public string Sex
{
get { return sex; }
set { sex = value; }
} private List<Courses> coursesLists = new List<Courses>();
/// <summary>
/// 课程列表
/// </summary>
public List<Courses> CoursesLists
{
get { return coursesLists; }
set { coursesLists = value; }
}
}
/// <summary>
/// 课程实体类
/// </summary>
public class Courses
{
private double score;
/// <summary>
/// 课程分数
/// </summary>
public double Score
{
get { return score; }
set { score = value; }
} private string course;
/// <summary>
/// 课程名称
/// </summary>
public string Course
{
get { return course; }
set { course = value; }
}
}
 /// <summary>
/// 张山的课程列表
/// </summary>
/// <returns></returns>
private static List<Courses> ZhangSan()
{
List<Courses> zhangsan = new List<Courses>();
Courses shuxue = new Courses();
shuxue.Course = "数学";
shuxue.Score = ;
zhangsan.Add(shuxue); Courses yingyu = new Courses();
yingyu.Course = "英语";
yingyu.Score = ;
zhangsan.Add(yingyu); Courses yuwen = new Courses();
yuwen.Course = "语文";
yuwen.Score = ;
zhangsan.Add(yuwen); Courses dili = new Courses();
dili.Course = "地理";
dili.Score = ;
zhangsan.Add(dili); return zhangsan;
} /// <summary>
/// 李氏的课程列表
/// </summary>
/// <returns></returns>
private static List<Courses> LiSi()
{
List<Courses> lisi = new List<Courses>();
Courses shuxue = new Courses();
shuxue.Course = "数学";
shuxue.Score = ;
lisi.Add(shuxue); Courses yingyu = new Courses();
yingyu.Course = "英语";
yingyu.Score = ;
lisi.Add(yingyu); Courses yuwen = new Courses();
yuwen.Course = "语文";
yuwen.Score = ;
lisi.Add(yuwen); Courses dili = new Courses();
dili.Course = "地理";
dili.Score = ;
lisi.Add(dili); return lisi;
} /// <summary>
/// 王武的课程列表
/// </summary>
/// <returns></returns>
private static List<Courses> WangWu()
{
List<Courses> wangwu = new List<Courses>();
Courses shuxue = new Courses();
shuxue.Course = "数学";
shuxue.Score = ;
wangwu.Add(shuxue); Courses yingyu = new Courses();
yingyu.Course = "英语";
yingyu.Score = ;
wangwu.Add(yingyu); Courses yuwen = new Courses();
yuwen.Course = "语文";
yuwen.Score = ;
wangwu.Add(yuwen); return wangwu;
} /// <summary>
/// 赵刘的课程列表
/// </summary>
/// <returns></returns>
private static List<Courses> ZhaoLiu()
{
List<Courses> zhaoliu = new List<Courses>();
Courses shuxue = new Courses();
shuxue.Course = "数学";
shuxue.Score = ;
zhaoliu.Add(shuxue); Courses yingyu = new Courses();
yingyu.Course = "英语";
yingyu.Score = ;
zhaoliu.Add(yingyu); Courses yuwen = new Courses();
yuwen.Course = "语文";
yuwen.Score = ;
zhaoliu.Add(yuwen); return zhaoliu;
}
  /// <summary>
/// 得到学生列表数据
/// </summary>
/// <returns></returns>
public static List<Students> GetStudentLists()
{
List<Students> studentLists = new List<Students>();
studentLists.Add(new Students()
{
Name = "张山",
Age = ,
Sex = "男",
CoursesLists = ZhangSan()
});
studentLists.Add(new Students()
{
Name = "李氏",
Age = ,
Sex = "女",
CoursesLists = LiSi()
});
studentLists.Add(new Students()
{
Name = "王武",
Age = ,
Sex = "男",
CoursesLists = WangWu()
});
studentLists.Add(new Students()
{
Name = "赵刘",
Age = ,
Sex = "女",
CoursesLists = ZhaoLiu()
}); return studentLists;
} /// <summary>
/// Silverlight页面初始化
/// </summary>
public DataGridMergeRows()
{
InitializeComponent();
dataGrid.ItemsSource = GetStudentLists();
}

以下是Silverlight UI页面代码

   <sdk:DataGrid x:Name="dataGrid" HorizontalAlignment="Center"  AutoGenerateColumns="False"  Height="" Margin="10,0,0,0" VerticalAlignment="Top" Width="">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="姓名" Binding="{Binding Name}" IsReadOnly="True" Width=""/>
<sdk:DataGridTextColumn Header="性别" Binding="{Binding Sex}" IsReadOnly="True" Width=""/>
<sdk:DataGridTextColumn Header="年龄" Binding="{Binding Age}" IsReadOnly="True" Width=""/> <sdk:DataGridTemplateColumn Header="课程" IsReadOnly="True" Width="" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=CoursesLists}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border MinHeight="" BorderThickness="0,0,0,1" Margin="0,0,0,-1" BorderBrush="#FFC9CACA">
<TextBlock Text="{Binding Path=Course}" VerticalAlignment="Center" Margin="" HorizontalAlignment="Left" TextWrapping="NoWrap"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn> <sdk:DataGridTemplateColumn Header="成绩" IsReadOnly="True" Width="" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=CoursesLists}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border MinHeight="" BorderThickness="0,0,0,1" Margin="0,0,0,-1" BorderBrush="#FFC9CACA">
<TextBlock Text="{Binding Path=Score}" VerticalAlignment="Center" Margin="" HorizontalAlignment="Left" TextWrapping="NoWrap"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns>
</sdk:DataGrid>