WPF DataGrid 之数据绑定
1. Auto generation of columns
最简单的方法莫过于让DataGrid根据数据源中的字段自动生成列了:
根据实体类的公共属性, 能够自动生成四种类型的数据列,对应关系如下:
TextBox columns for string values;
CheckBox columns for boolean values;
ComboBox columns for enumerable values;
Hyperlink columns for Uri values;
拖个DataGrid放在Window中, 把他的ItemsSource绑定到数据源上
如:ItemsSource="{Binding Customers}"
注意:一定要把AutoGenerateColumns的值改为True, 默认拖上去时此属性的值为True.
2. Manually define columns
设置AutoGenerateColumns为False, 那么一切就要靠自己了!
此时需要在xaml文件中编辑DataGrid.Columns。
同样我们可以这样对应数据类型:
DataGridCheckBoxColumn for boolean values;
DataGridComboBoxColumn for enumerable values;
DataGridHyperlinkColumn for Uri values;
DataGridTextColumn to show text values;
DataGridTemplateColumn to show any types of data by defining your own cell template.(Import for customer defined columns)
比如要定义一个显示图片的列: <DataGrid.Columns>
<DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="80" Source="{Binding Image}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
3. Selection
通过设置DataGrid的SelectionMode,SelectionUnit属性,可以控制运行时我们能过选中的Cell or Row 的模式。
SelectionMode.Single 每次只能选中单行;
SelectionMode.Extended 可以选中多行;
SelectionUnit.Cell 每次只能选中cell;
SelectionUnit.FullRow 每次只能选中row;
SelectionUnit.CellorRowHeader 可选中cell,也可通过RowHeader选中row.
4. Column sorting, reordering and resizing
我们可以通过几个简单的属性实现很酷的功能:
CanUserReorderColumns enables or disables column re-ordering
CanUserResizeColumns enables or disables column resizing
CanUserResizeRows enables or disables row resizing
CanUserSortColumns enables or disables column sorting
CanUserAddRows boolean ---是否允许最后一行有增加项
(These properties’ default values are “True”), like:
<DataGrid ItemsSource="{Binding Customers}"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="True”
CanUserSortColumns="True"/>
5. Grouping
首先要使用能够分组的数据源,见demo.
Like this:
GroupedCustomers = new ListCollectionView(Customers);
GroupedCustomers.GroupDescriptions.Add(new PropertyGroupDescription("Gender"));
重要的是你还要在xaml文件中定义模板来显示分组, It should be like this:
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding GroupedCustomers}">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Items"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
6. Row Details
这个比较好搞,只要设置 RowDetailsTemplate就可以了!
It is like this:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Image Height="100" Source="{Binding Image}" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
通过实现RowDetailsTemplateSelector
,还可以根据条件生成不同的Details(Row Details depending on the type of data).
代码有点多,like bellow:
public class GenderTemplateSelector : DataTemplateSelector
{
public DataTemplate MaleTemplate { get; set; }
public DataTemplate FemaleTemplate { get; set; }
public override DataTemplate SelectTemplate(object item,
DependencyObject container)
{
var customer = item as Customer;
if (customer == null)
return base.SelectTemplate(item, container);
if( customer.Gender == Gender.Male)
{
return MaleTemplate;
}
return FemaleTemplate;
}
}
<l:GenderTemplateSelector x:Key="genderTemplateSelector">
<l:GenderTemplateSelector.MaleTemplate>
<DataTemplate>
<Grid Background="LightBlue">
<Image Source="{Binding Image}" Width="50" />
</Grid>
</DataTemplate>
</l:GenderTemplateSelector.MaleTemplate>
<l:GenderTemplateSelector.FemaleTemplate>
<DataTemplate>
<Grid Background="Salmon">
<Image Source="{Binding Image}" Width="50" />
</Grid>
</DataTemplate>
</l:GenderTemplateSelector.FemaleTemplate>
</l:GenderTemplateSelector> <DataGrid ItemsSource="{Binding Customers}"
RowDetailsTemplateSelector="{StaticResource genderTemplateSelector}" />
7. Alternating BackgroundBrush
可以通过AlternatingRowBackground and AlternationCount 两个属性轻松实现隔行背景色。
8. Frozen Columns
通过设置FrozenColumnCount 属性,可以控制Frozen Columns.
数字 1 对应第一列,2 对应第二列,以此类推
9. Headers visbility
使用HeadersVisibility控制是否显示列头。
WPF DataGrid 之数据绑定的更多相关文章
- WPF DataGrid 之数据绑定--实例2
1.前台Grid定义 <!--数据绑定--> <DataGrid Grid.Row="1" Name="gridOne" Margin=&qu ...
- WPF DataGrid常用属性记录
WPF DataGrid常用属性记录 组件常用方法: BeginEdit:使DataGrid进入编辑状态. CancelEdit:取消DataGrid的编辑状态. CollapseRowGroup:闭 ...
- 编写 WPF DataGrid 列模板,实现更好的用户体验
Julie Lerman 下载代码示例 最近我在为一个客户做一些 Windows Presentation Foundation (WPF) 方面的工作. 虽然我提倡使用第三方工具,但有时也会避免使用 ...
- WPF Datagrid 动态生成列 并绑定数据
原文:WPF Datagrid 动态生成列 并绑定数据 说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 数据来源于左侧列 左侧列数据源 当然num1 属于临时的dome使用 可 ...
- WPF入门教程系列十五——WPF中的数据绑定(一)
使用Windows Presentation Foundation (WPF) 可以很方便的设计出强大的用户界面,同时 WPF提供了数据绑定功能.WPF的数据绑定跟Winform与ASP.NET中的数 ...
- WPF中的数据绑定!!!
引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx 数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...
- WPF DATAGRID - COMMITTING CHANGES CELL-BY-CELL
In my recent codeproject article on the DataGrid I described a number of techniques for handling the ...
- EasyUI DataGrid分页数据绑定
记录东西感觉很痛苦,总结东西很痛苦,麻烦,不过为了下次的方便和知识的牢固以后要坚持总结. EasyUI DataGrid分页数据绑定 在解决方案中新建两个文件FormMain.aspx(html也可以 ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
随机推荐
- JAVA-2-GetDay
import java.util.*; public class Ch0310 { public static void main(String[] args) { // TODO 自动生成的方法存根 ...
- 3D dungeon
算法:广搜: 描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is comp ...
- addChildViewController 用法
// // SCMyOrderViewController.m // SmartCommunity // // Created by chenhuan on 15/9/7. // Copyright ...
- 【回忆1314】第一次用AngularJS
1.创建指令的4种方式(ECMA) var appModule = angular.module('app', []); appModule.directive('hello', function() ...
- 转:BZERO()等的区别
BZERO()等的区别 bzero 原型: extern void bzero(void *s, int n); 用法: #include <string.h> 功能:置字节字符串s的前 ...
- Linux学习——粘粘今天看的东西
由二分割表就叧有64 bytes而已,最多叧能容纳四笔分割的记录, 这四个分割的记录被称为主要(Primary)戒延伸(Extended)分割槽.分割槽的最小单位为磁柱(cylinder)请注意, 延 ...
- PHP 中的静态变量的简单使用
静态变量的初始化只能在第一次static 声明的时候进行,这些静态变量只能在声明他的函数中访问到. 例如: <?php function do_something(){ static $firs ...
- LED驅動電路概述
LED是一種固體光源,當它兩端加上正向電壓,半導體中的少數載流子和多數載流子發生復合,放出的過剩能量將引起光子發射.采用不同的材料,可制成不同顏色有 發光二極管 . 隨著對LED研究的進一步深入,其光 ...
- Altium Designer如何批量修改名称,数值,封装
方法一: altium里的封装管理库 1,Tools -> Footprint Manager -> ...2,在Component List里选择要改的器件3,在View and Edi ...
- MFC的GUI窗口使用Console输出函数printf
在GUI程序中使用printf函数: #include <io.h> #include <fcntl.h> void InitConsole() { ; FILE* fp; A ...