http://www.cnblogs.com/tnspop/archive/2012/01/10/2317803.html
Silverlight中DataGrid控件提供了三种模板分别是(DataGridCheckBoxColumn,DataGridTextColumn,DataGridTemplateColumn,RowDetailsTemplates)
这里主要讲解一下DataGridTemplateColumn,RowDetailsTemplate模板的使用方法
在DataGridTemplateColumn里分别又有显示模板CellTemplate和编辑模板CellEditingTemplate.
在编辑模板中我们可以添加任意编辑控件
在RowDetailsTemplate中主要显示一些详细的内容.
数据对象
View
Code
/// <summary> /// DataGrid自定义列 /// </summary> public class BookClass2 { /// <summary> /// 是否选择 /// </summary> public bool IsSelect { get; set; } /// <summary> /// 书名 /// </summary> public string Title { get; set; } /// <summary> /// 作者 /// </summary> public string Author { get; set; } /// <summary> /// 发布日期 /// </summary> public DateTime Time { get; set; } /// <summary> /// 作者列表 /// </summary> public List<Auth> AuthorList { get; set; } public string Entity { get; set; } /// <summary> /// 实例化 /// </summary> /// <param name="isselect">是否选择</param> /// <param name="title">书名</param> /// <param name="author">作者</param> public BookClass2(bool isselect, string title, string author,string entity) { IsSelect = isselect; Title = title; Author = author; Entity = entity; Time = DateTime.Now; AuthorList = Auth.List; } } /// <summary> /// 作者类 /// </summary> public class Auth { public string AuthName { get; set; } public static List<Auth> List = new List<Auth>() { new Auth { AuthName = "tnspop" }, new Auth { AuthName = "tnsstar" }, new Auth { AuthName = "tnssun" } }; }
DataGrid数据控件
View
Code
<UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="DataGridTest.Control.DataGridBook2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="150"> <Grid x:Name="LayoutRoot" Background="White"> <data:DataGrid Name="gridSetColumns" AutoGenerateColumns="False"> <data:DataGrid.Columns> <data:DataGridCheckBoxColumn Header="操作" Binding="{Binding IsSelect}"/> <data:DataGridTextColumn Header="书名" Binding="{Binding Title}"/> <data:DataGridTemplateColumn Header="作者"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Author}"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding AuthorList}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding AuthName}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="发布日期"> <data:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Time}"/> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <controls:DatePicker SelectedDate="{Binding Time}"></controls:DatePicker> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> <data:DataGrid.RowDetailsTemplate> <DataTemplate> <TextBlock Text="{Binding Entity}"/> </DataTemplate> </data:DataGrid.RowDetailsTemplate> </data:DataGrid> </Grid> </UserControl>
后台cs代码
View
Code
public partial class DataGridBook2 : UserControl { public DataGridBook2() { InitializeComponent(); Databinding(); } private void Databinding() { List<BookClass2> list = new List<BookClass2>() { new BookClass2(true, "坏蛋是怎样炼成的", "tnspop","这本书讲了谢文东是如何从一个弱者变成一个强者的"), new BookClass2(false, "咸郎平说", "tnsstar","这本书讲了一些常见的经济问题,分析的很透彻"), new BookClass2(true, "帝国主义在中国", "tnssun","这本书讲了那些资本投资的金融学家在中国的一些见不得光的事情") }; gridSetColumns.ItemsSource = list; } }
至此,DataGrid数据自定义列完毕,下一节将讲如何在dataGrid中操作数据对象,数据绑定模式和对象集合绑定
分类:
项目积累
标签:
DataGrid,
silverlight