给大家讲到WPF,我们就不得不提到WinForm,51CTO之前也报导过《WPF与WinForm对比谈 多线程编程优化是关键》的技术文章,对于他们之间的优劣有了更深刻的认识。而今天我们要讲到的是WPF 4中的DataGrid空间。我们也诚挚向您推荐《让你的代码“炫”起来——WPF开发教程》。
提到DataGrid 不管是网页还是应用程序开发都会频繁使用。通过它我们可以灵活的在行与列间显示各种数据。本篇将详细介绍WPF 4 中DataGrid 的相关功能。
自定义列
默认情况下,当我们为DataGrid 控件设置ItemSource 属性后,DataGrid 会根据数据类型自动生成相应的列,下表列出DataGrid 支持的四种列及其数据类型。
在创建DataGrid 时可以通过AutoGenerateColumns 属性设置列是否自动生成,从而加入自定义列。如果DataGrid 中同时包含“自动生成列”与“用户自定义列”,则首先创建“用户自定义列”。下面代码实例中分别创建这四种列:
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">Window</span><span> </span><span class="attribute">x:Class</span><span>=</span><span class="attribute-value">"WPF4ControlTest.MainWindow"</span><span> </span></span></li><li><span> </span><span class="attribute">xmlns</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:x</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span> </span></li><li><span> </span><span class="attribute">xmlns:local</span><span>=</span><span class="attribute-value">"clr-namespace:WPF4ControlTest"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:assembly</span><span>=</span><span class="attribute-value">"clr-namespace:System;assembly=mscorlib"</span><span> </span></li><li><span> </span><span class="attribute">Title</span><span>=</span><span class="attribute-value">"MainWindow"</span><span> </span><span class="attribute">Height</span><span>=</span><span class="attribute-value">"200"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"500"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider</span><span> </span><span class="attribute">x:Key</span><span>=</span><span class="attribute-value">"sexEnum"</span><span> </span><span class="attribute">MethodName</span><span>=</span><span class="attribute-value">"GetValues"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">ObjectType</span><span>=</span><span class="attribute-value">"{x:Type assembly:Enum}"</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">x:Type</span><span> </span><span class="attribute">Type</span><span>=</span><span class="attribute-value">"local:SexOpt"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Name"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Name}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Age"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"50"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Age}"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridComboBoxColumn</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Sex"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">SelectedItemBinding</span><span>=</span><span class="attribute-value">"{Binding Sex}"</span><span> </span></li><li><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding Source={StaticResource sexEnum}}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridCheckBoxColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Pass Exam?"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"100"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Pass}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridHyperlinkColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Email"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"150"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Email}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">DataGrid</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span class="tag"></</span><span class="tag-name">Window</span><span class="tag">></span><span> </span></li></ol> |
其中每列都包含各自的数值类型,在C# 中创建Member 类及SexOpt 枚举,并将memberData 数据绑定到DataGrid:
1 |
<ol class="dp-c"><li class="alt"><span><span class="keyword">using</span><span> System; </span></span></li><li><span class="keyword">using</span><span> System.Windows; </span></li><li class="alt"><span class="keyword">using</span><span> System.Collections.ObjectModel; </span></li><li><span> </span></li><li class="alt"><span class="keyword">namespace</span><span> WPF4ControlTest </span></li><li><span>{ </span></li><li class="alt"><span> </span><span class="comment">/// <summary> </span><span> </span></li><li><span> </span><span class="comment">/// Interaction logic for MainWindow.xaml </span><span> </span></li><li class="alt"><span> </span><span class="comment">/// </summary> </span><span> </span></li><li><span> </span><span class="keyword">public</span><span> partial </span><span class="keyword">class</span><span> MainWindow : Window </span></li><li class="alt"><span> { </span></li><li><span> </span><span class="keyword">public</span><span> MainWindow() </span></li><li class="alt"><span> { </span></li><li><span> InitializeComponent(); </span></li><li><span>ObservableCollection<Member> memberData = </span><span class="keyword">new</span><span> ObservableCollection<Member>(); </span></li><li><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li class="alt"><span> { </span></li><li><span> Name = </span><span class="string">"Joe"</span><span>, Age = </span><span class="string">"23"</span><span>, Sex = SexOpt.Male, </span></li><li class="alt"><span> Pass = </span><span class="keyword">true</span><span>, Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Joe@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Mike"</span><span>, Age = </span><span class="string">"20"</span><span>, </span></li><li><span> Sex = SexOpt.Male, Pass = </span><span class="keyword">false</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Mike@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Lucy"</span><span>, Age = </span><span class="string">"25"</span><span>, </span></li><li><span> Sex = SexOpt.Female, Pass = </span><span class="keyword">true</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Lucy@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> dataGrid.DataContext = memberData; </span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">enum</span><span> SexOpt { Male, Female }; </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Member </span></li><li><span> { </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Name { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Age { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> SexOpt Sex { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">bool</span><span> Pass { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> Uri Email { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> } </span></li><li class="alt"><span>} </span></li></ol> |
这样我们就可以通过自定义列的方式创建出DataGrid:
选择模式
默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改DataGrid 的选择模式。
SelectionUnit:包含Cell、FullRow 和CellOrRowHeader 三种单元选择模式。
◆Cell:选择单元格;
◆FullRow:选择全行;
◆CellOrRowHeader:可选择单元格,也可以通过点击行首选择全行;
SelectionMode:分为Extended 和Single 两种模式。
◆Extended:选择多个单元(单元格或全行,由SelectionUnit 定义);
◆Single:选择唯一单元(单元格或全行,由SelectionUnit 定义);
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span class="tag">></span><span>… … </span></li></ol> |
设置SelectionUnit 和SelectionMode 后的实例效果:
编辑
默认情况下,我们可以直接在DataGrid 中编辑数据(下表为相关的编辑命令),当然也可以通过IsReadOnly 属性将DataGrid 设置为只读方式。
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span> </span><span class="attribute">IsReadOnly</span><span>=</span><span class="attribute-value">"True"</span><span class="tag">></span><span>… … </span></li></ol> |
原文标题:WPF 4 DataGrid 控件(基本功能篇)
链接:http://www.cnblogs.com/gnielee/archive/2010/04/16/wpf4-datagrid-control-basic.html