一、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; }
效果如下:
二、模擬合併單元格
在本實例中我們通過對數據源進行構造,然後在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; }
效果如下:
三、實體類如下:
/// <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 下載