欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Silverlight的DataGrid合并单元格

程序员文章站 2022-04-08 14:50:59
现在也不知道还有没有同学做Silverlight开发了,我是一个Silverlight菜鸟,遇到问题也很难百度查到。就简单的记录一下这两天遇到的问题,并做了一个简单的小Demo,希望能够帮助到其他同学,并为自己翻看。 DataGrid合并单元格,实现结果如下: Silverlight后台代码如下: ......

     现在也不知道还有没有同学做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 = 88;
            zhangsan.Add(shuxue);

            Courses yingyu = new Courses();
            yingyu.Course = "英语";
            yingyu.Score = 89;
            zhangsan.Add(yingyu);

            Courses yuwen = new Courses();
            yuwen.Course = "语文";
            yuwen.Score = 87;
            zhangsan.Add(yuwen);

            Courses dili = new Courses();
            dili.Course = "地理";
            dili.Score = 86;
            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 = 90;
            lisi.Add(shuxue);

            Courses yingyu = new Courses();
            yingyu.Course = "英语";
            yingyu.Score = 95;
            lisi.Add(yingyu);

            Courses yuwen = new Courses();
            yuwen.Course = "语文";
            yuwen.Score = 98;
            lisi.Add(yuwen);

            Courses dili = new Courses();
            dili.Course = "地理";
            dili.Score = 97;
            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 = 77;
            wangwu.Add(shuxue);

            Courses yingyu = new Courses();
            yingyu.Course = "英语";
            yingyu.Score = 78;
            wangwu.Add(yingyu);

            Courses yuwen = new Courses();
            yuwen.Course = "语文";
            yuwen.Score = 79;
            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 = 90;
            zhaoliu.Add(shuxue);

            Courses yingyu = new Courses();
            yingyu.Course = "英语";
            yingyu.Score = 95;
            zhaoliu.Add(yingyu);

            Courses yuwen = new Courses();
            yuwen.Course = "语文";
            yuwen.Score = 98;
            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 = 20,
                Sex = "男",
                CoursesLists = ZhangSan()
            });
            studentLists.Add(new Students()
            {
                Name = "李氏",
                Age = 25,
                Sex = "女",
                CoursesLists = LiSi()
            });
            studentLists.Add(new Students()
            {
                Name = "王武",
                Age = 35,
                Sex = "男",
                CoursesLists = WangWu()
            });
            studentLists.Add(new Students()
            {
                Name = "赵刘",
                Age = 30,
                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="470" Margin="10,0,0,0" VerticalAlignment="Top" Width="911">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="姓名" Binding="{Binding Name}" IsReadOnly="True" Width="108"/>
                <sdk:DataGridTextColumn Header="性别" Binding="{Binding Sex}" IsReadOnly="True" Width="108"/>
                <sdk:DataGridTextColumn Header="年龄" Binding="{Binding Age}" IsReadOnly="True" Width="108"/>

                <sdk:DataGridTemplateColumn Header="课程" IsReadOnly="True" Width="108" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding Path=CoursesLists}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border MinHeight="25" BorderThickness="0,0,0,1" Margin="0,0,0,-1" BorderBrush="#FFC9CACA">
                                            <TextBlock Text="{Binding Path=Course}" VerticalAlignment="Center" Margin="5" HorizontalAlignment="Left" TextWrapping="NoWrap"/>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>

                <sdk:DataGridTemplateColumn Header="成绩" IsReadOnly="True" Width="108" >
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding Path=CoursesLists}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border MinHeight="25" BorderThickness="0,0,0,1" Margin="0,0,0,-1" BorderBrush="#FFC9CACA">
                                            <TextBlock Text="{Binding Path=Score}" VerticalAlignment="Center" Margin="5" HorizontalAlignment="Left" TextWrapping="NoWrap"/>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>

                
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>