温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Silverlight实用窍门系列:73.Silverlight的DataGrid分组以及模拟合并单元格

发布时间:2020-06-26 10:46:32 来源:网络 阅读:929 作者:程兴亮 栏目:编程语言

一、DataGrid分组

  在Silverlight的表格中可能需要对某一些数据进行分组以方便客户查看,我们使用PagedCollectionView集合作为数据源,然后通过设置其GroupDescriptions属性值,添加需要分组的实体集合属性,注意:如果需要两层分组则添加两个属性即可。

  Xaml代码:

        <sdk:DataGrid HorizontalAlignment="Left"  AutoGenerateColumns="False"   Name="ShowCityList" 
                      VerticalAlignment="Top"  Height="400"  >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="省会" Binding="{Binding AddrName}"  Width="108"/>
                <sdk:DataGridTextColumn Header="城市" Binding="{Binding CityName}" IsReadOnly="True" Width="108"/>
                <sdk:DataGridTextColumn Header="电话区号" Binding="{Binding TelNum}" IsReadOnly="True" Width="108"/>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

  Xaml.cs代码如下:

            PagedCollectionView view = new PagedCollectionView(CityInfo.GetInfo());
            //此处根据AddrName属性分组
            view.GroupDescriptions.Add(new PropertyGroupDescription("AddrName"));
            //如果需要多重分组,则取消以下注释
            //view.GroupDescriptions.Add(new PropertyGroupDescription("CityName"));
            this.ShowCityList.ItemsSource = view;

  实体数据源函数如下:

        public static List<CityInfo> GetInfo()
        {
            var list = new List<CityInfo>();
            list.Add(new CityInfo() { AddrName = "北京", CityName = "北京市", TelNum = "010" });
            list.Add(new CityInfo() { AddrName = "上海", CityName = "上海市", TelNum = "020" });
            list.Add(new CityInfo() { AddrName = "广东", CityName = "广州市", TelNum = "021" });
            list.Add(new CityInfo() { AddrName = "广东", CityName = "深圳市", TelNum = "0210" });
            list.Add(new CityInfo() { AddrName = "四川", CityName = "成都市", TelNum = "028" });
            list.Add(new CityInfo() { AddrName = "四川", CityName = "内江市", TelNum = "0832" });
            list.Add(new CityInfo() { AddrName = "四川", CityName = "自贡市", TelNum = "0831" });
            return list;
        }

  效果如下:

Silverlight实用窍门系列:73.Silverlight的DataGrid分组以及模拟合并单元格

二、模拟合并单元格

  在本实例中我们通过对数据源进行构造,然后在DataGrid控件上设置DataGridTemplateColumn的模板方式模拟合并单元格。

  Xaml代码:

        <sdk:DataGrid HorizontalAlignment="Left"  AutoGenerateColumns="False"   Name="ShowCity"
                      VerticalAlignment="Top"  Height="400"  Margin="400 0 0 0" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="省会" Binding="{Binding AddrName}" IsReadOnly="True" Width="108"/>
                <sdk:DataGridTemplateColumn Header="城市" Width="108">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding CityNames}"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
                <sdk:DataGridTemplateColumn Header="电话区号" Width="108">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ItemsControl ItemsSource="{Binding TelNums}" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

  Xaml.cs代码:

this.ShowCity.ItemsSource = CityInfo.GetInfoList();

  实体数据源函数如下:

        public static List<CityInfo> GetInfoList()
        {
            var list = new List<CityInfo>();
            list.Add(new CityInfo() { AddrName = "北京", CityNames = new List<string>() { "北京市" },
                TelNums = new List<string>() { "010" } });
            list.Add(new CityInfo() { AddrName = "上海", CityNames = new List<string>() { "上海市" },
                TelNums = new List<string>() { "020" } });
            list.Add(new CityInfo() { AddrName = "广东", CityNames = new List<string>() { "广州市", "深圳市" },
                TelNums = new List<string>() {  "021","0210" } });
            list.Add(new CityInfo() { AddrName = "四川", CityNames = new List<string>() { "成都市", "内江市","自贡市" }, 
                TelNums = new List<string>() { "028","0832","0831" } });
            return list;
        }

  效果如下:

Silverlight实用窍门系列:73.Silverlight的DataGrid分组以及模拟合并单元格

三、实体类如下:

    /// <summary>
    /// 城市信息的实体类
    /// </summary>
    public class CityInfo
    {
        private string _AddrName;
        private string _CityName;
        private string _TelNum;

        public string AddrName
        {
            get { return _AddrName; }
            set { _AddrName = value; }
        }
        public string CityName
        {
            get { return _CityName; }
            set { _CityName = value; }
        }

        public string TelNum
        {
            get { return _TelNum; }
            set { _TelNum = value; }
        }


        private List<string> _CityNames;
        public List<string> CityNames
        {
            get { return _CityNames; }
            set { _CityNames = value; }
        }

        private List<string> _TelNums;

        public List<string> TelNums
        {
            get { return _TelNums; }
            set { _TelNums = value; }
        }
}

  本实例需要引用System.Windows.Data.dll程序集,如需源码请点击 SLDataGrid.rar 下载 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI