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

一、DataGrid分组

我们提供的服务有:成都做网站、网站建设、微信公众号开发、网站优化、网站认证、鄞州ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的鄞州网站制作公司

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

Xaml代码:

        
            
                
                
                
            
        

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 GetInfo()
        {
   var list = new List();
            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代码:

        
            
                
                
                    
                        
                            
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
        

Xaml.cs代码:

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

实体数据源函数如下:

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

效果如下:

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

三、实体类如下:

    /// 
    /// 城市信息的实体类
/// 
    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 _CityNames;
public List CityNames
        {
   get { return _CityNames; }
   set { _CityNames = value; }
        }

private List _TelNums;

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

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


新闻名称:Silverlight实用窍门系列:73.Silverlight的DataGrid分组以及模拟合并单元格
文章源于:http://hbruida.cn/article/pojedg.html