强大的DataGrid组件[7]_自定义DataGrid——Silverlight学习笔记[15]
基本知识讲解
1)两种状态
DataGrid的单元格的状态有两类,即编辑状态和非编辑状态。
在实际开发中,如果一个单元格所在的列不设为只读的话(即要求可读写),那么这个单元格就存在这两种状态。按需要,这时就应当对这两种状态对单元格分别设定不同的编辑模板。如果该单元格仅用于进行简易的文本编辑,则可保留原有状态,无需重新设定。
这两种编辑模板的标签如下所示:
①非编辑状态模板:<data:DataGridTemplateColumn.CellTemplate>
②编辑状态模板:<data:DataGridTemplateColumn.CellEditingTemplate>
2)三种模板
①<data:DataGridTextColumn>
普通文本列,即基本默认设置
②<data:DataGridCheckBoxColumn>
带有复选框的列,当该列单元格数据的值为true或false、1或0时,将该列的模板设定。
③<data:DataGridTemplateColumn>
自定义模板列,这个是功能最强的可以放入任何自定义控件。
关于绑定
如果单元格所在列无需编辑或只读的话,绑定模式设定为默认的OneWay即可。如果该列需要进行编辑,就请将绑定模式设为TwoWay。
更为详细的说明请参见MSDN的文章。(点这里)
实例
说明:为了能自定义列,我们需要先将DataGrid的AutoGenerateColumns属性设为false。
MainPage.xaml文件代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<UserControl xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:src="clr-namespace:SilverlightClient" mc:Ignorable="d" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightClient.MainPage" d:DesignWidth="320" d:DesignHeight="240"><UserControl.Resources> <!--静态资源绑定--> <src:cbSexListProvider x:Key="cbSexListProvider"></src:cbSexListProvider> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" Width="320" Height="240"> <data:DataGrid x:Name="dgCustom" Margin="8,8,8,42" AutoGenerateColumns="False" FontSize="14"> <data:DataGrid.Columns> <data:DataGridTextColumn Header="编号" IsReadOnly="True" Binding="{Binding EmployeeID,Mode=OneWay}" /><!--该列只读--> <data:DataGridTextColumn Header="名称" Binding="{Binding EmployeeName,Mode=TwoWay}" /> <data:DataGridTextColumn Header="年龄" Binding="{Binding EmployeeAge,Mode=TwoWay}" /> <data:DataGridTemplateColumn Header="性别" Width="80"> <data:DataGridTemplateColumn.CellTemplate><!--普通显示模式--> <DataTemplate> <TextBlock Text="{Binding EmployeeSex}"></TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellTemplate> <data:DataGridTemplateColumn.CellEditingTemplate><!--编辑模式--> <DataTemplate> <ComboBox Width="80" ItemsSource="{Binding cbSexList,Source={StaticResource cbSexListProvider}}" SelectedItem="{Binding EmployeeSex,Mode=TwoWay}" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridCheckBoxColumn Header="婚否" Binding="{Binding EmployeeMarried,Mode=TwoWay}" /> </data:DataGrid.Columns> </data:DataGrid> </Grid></UserControl> |
MainPage.xaml.cs文件代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace SilverlightClient{ //静态资源绑定源 public class cbSexListProvider { public List<string> cbSexList { get { return new List<string> { "男", "女" }; } } } //定义数据类 public class Employees { public int EmployeeID { get; set; } public string EmployeeName { get; set; } public int EmployeeAge { get; set; } public string EmployeeSex { get; set; } public int EmployeeMarried { get; set; } } public partial class MainPage : UserControl { List<Employees> em = new List<Employees>(); public MainPage() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { em.Add(new Employees() { EmployeeID = 1, EmployeeName = "张三", EmployeeAge = 23, EmployeeSex = "男", EmployeeMarried = 0 }); em.Add(new Employees() { EmployeeID = 2, EmployeeName = "李四", EmployeeAge = 24, EmployeeSex = "女", EmployeeMarried = 1 }); em.Add(new Employees() { EmployeeID = 3, EmployeeName = "王五", EmployeeAge = 25, EmployeeSex = "男", EmployeeMarried = 1 }); dgCustom.ItemsSource = em; } }} |
最终效果图:

文章出处:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)

浙公网安备 33010602011771号