wpf如何gridview绑定数据源源中具体某行某列的数据?

WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法
WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法
  最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性:    然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位    起初还以为是显卡的问题,结果今天来集成显卡的电脑上也一样出现,具体原因不详,以下是几种解决办法:  1.(首选,有效果)DataGrid的RowHeaderWidth=&0&,必须赋值为0,不能不赋值,也不能赋其他值。  2.(有效果)绑定的源一开始就要赋值,即至少要有一行数据。  3.(效果不一,有的地方有效有的地方无效)不用绑定,每次获得数据后手动赋ItemsSource的值。  4.(有效,比较繁琐)把每一条数据封装成对象,DataGrid封装成列模板,每一列绑定这个对象的属性。  对于使用DataTable还是用集合做为数据源,总结以下几点:  a)绑定DataTable的DefaultView有几个个好处:  1.方便,直接取数绑给目标,不需要再转成集合给目标每一列绑定。  2.易替换,如果要增减列,只需要改取数语句,界面不需改动。  3.相对于转换成集合来说理论上节省性能。  4.在大数据量下,如果前后两次获取的数据源没有变动,不会重绘UI,而集合的话每次都会重绘。  缺陷:  1.需要代码设置每一列的列宽,否则UI会很难看,全挤在一起。(代码设置的过程可能会消耗性能)。  2.无数据时UI的数据区域什么都没有,一片空白,影响用户体验。  适用的地方:一般是使用在数据量大、不需要操作数据的地方,只是单纯的显示。  b)绑定集合的好处:  1.UI里可以直接设置列宽,不需要代码再设。  2.不存在列位移的现象(至今未发现)。  3.无数据时UI上也会有列头。  4.可以灵活使用列模板。  缺陷:  1.UI和集合数据列绑死,增减列需要动UI。  2.取到数据之后还要转换成集合,理论上耗能。  3.步骤繁琐,需要将数据条目封装成对象,如果字段很多就烦了。  适用的地方:数据量不大、需要操作数据,比如要加一列CheckBox标识选中,加列ComBox提供候选等。  作者: 热卡出处:
H3C认证Java认证Oracle认证
基础英语软考英语项目管理英语职场英语
.NETPowerBuilderWeb开发游戏开发Perl
二级模拟试题一级模拟试题一级考试经验四级考试资料
软件测试软件外包系统分析与建模敏捷开发
法律法规历年试题软考英语网络管理员系统架构设计师信息系统监理师
高级通信工程师考试大纲设备环境综合能力
路由技术网络存储无线网络网络设备
CPMP考试prince2认证项目范围管理项目配置管理项目管理案例项目经理项目干系人管理
职称考试题目
招生信息考研政治
网络安全安全设置工具使用手机安全
生物识别传感器物联网传输层物联网前沿技术物联网案例分析
Java核心技术J2ME教程
Linux系统管理Linux编程Linux安全AIX教程
Windows系统管理Windows教程Windows网络管理Windows故障
数据库开发Sybase数据库Informix数据库
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&WPF的数据绑定详细介绍
字体:[ ] 类型:转载 时间:
数据绑定:是应用程序 UI 与业务逻辑之间建立连接的过程。 如果绑定正确设置并且数据提供正确通知,则当数据的值发生更改时,绑定到数据的视觉元素会自动反映更改。 数据绑定可能还意味着如果视觉元素中数据的外部表现形式发生更改,则基础数据可以自动更新以反映更改。
一、WPF数据绑定的概要
数据绑定:是应用程序 UI 与业务逻辑之间建立连接的过程。 如果绑定正确设置并且数据提供正确通知,则当数据的值发生更改时,绑定到数据的视觉元素会自动反映更改。 数据绑定可能还意味着如果视觉元素中数据的外部表现形式发生更改,则基础数据可以自动更新以反映更改。
例如:如果用户编辑 TextBox 元素中的值,则基础数据值会自动更新以反映该更改。
1. 数据绑定涉及到两个方面:
一个是绑定源,一个是绑定目标。绑定源即控件绑定所使用的源数据,绑定目标即数据显示的控件。
2. 对于绑定源,在WPF可以是以下四种:
•CLR对象:可以绑定到CLR类的公开的属性、子属性、索引器上。 •ADO.Net对象:例如DataTable、DataView等 。•XML文件:使用XPath进行解析 。•DependencyObject:绑定到其依赖项属性上,即控件绑定控件 。对于绑定目标,必须是WPF中的DependencyObject,将数据绑定到其依赖项属性上。
二、&&&&& 绑定的模式
1.& 根据数据流的方向,WPF中的数据绑定分为以下四种:
OneWay 绑定:对源属性的更改会自动更新目标属性,但是对目标属性的更改不会传播回源属性。此绑定类型适用于绑定的控件为隐式只读控件的情况。
TwoWay 绑定:对源属性的更改会自动更新目标属性,而对目标属性的更改也会自动更新源属性。此绑定类型适用于可编辑窗体或其他完全交互式 UI 方案 。
OneWayToSource 与 OneWay 相反;它在目标属性更改时更新源属性。
OneTime绑定:该绑定会导致源属性初始化目标属性,但不传播后续更改。
注释:如果无需监视目标属性的更改,则使用 OneWay 绑定模式可避免 TwoWay 绑定模式的系统开销。
大多数属性都默认为 OneWay 绑定,但是一些依赖项属性,通常为用户可编辑的控件的属性,如 TextBox 的 Text 属性和 CheckBox 的 IsChecked 属性,默认为 TwoWay 绑定。
如果要知道依赖项属性绑定在默认情况下是单向还是双向的编程方法可使用 GetMetadata 获取属性的属性元数据,然后检查 BindsTwoWayByDefault 属性的布尔值。
示例代码:
代码如下:&Page x:Class="WpfDemo.Page1"
&&& xmlns="/winfx/2006/xaml/presentation"
&&& xmlns:x="/winfx/2006/xaml"
&&& Title="Page1" HorizontalAlignment="Center"&
&&&&&&& &Grid Name="GridTable" Height="360" Background="Silver"&
&&&&&&&&&&& &Grid.RowDefinitions&
&&&&&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &/Grid.RowDefinitions&
&&&&&&&&&&& &Grid.ColumnDefinitions&
&&&&&&&&&&& &ColumnDefinition Width="130"&&/ColumnDefinition&
&&&&&&&&&&& &ColumnDefinition Width="150"&&/ColumnDefinition&
&&&&&&&&&&& &ColumnDefinition Width="20"&&/ColumnDefinition&
&&&&&&&&&&& &/Grid.ColumnDefinitions&
&&&&&&& &Label Width="130" Height="25"& Grid.Row="0" Grid.Column="0"& Name="label1"&TwoWay&/Label&
&&&&&&& &TextBox Width="150" Height="25"& Grid.Row="0" Grid.Column="1"& Name="textBox4" Text="{Binding ElementName=scrollBar1,Path=Value,Mode=TwoWay}" /&
&&&&&&& &Label Width="130" Height="25"& Grid.Row="1" Grid.Column="0"& Name="label2"&OneWay&/Label&
&&&&&&& &TextBox Width="150" Height="25"& Grid.Row="1" Grid.Column="1"&& Name="textBox1" Text="{Binding ElementName=scrollBar1, Path=Value,Mode=OneWay}"/&
&&&&&&& &Label Width="130" Height="25"& Grid.Row="2" Grid.Column="0"& Name="label3"&OneWayToSource&/Label&
&&&&&&& &TextBox Width="150" Height="25"& Grid.Row="2" Grid.Column="1"&& Name="textBox2" Text="{Binding ElementName=scrollBar1, Path=Value,Mode=OneWayToSource}" /&
&&&&&&& &Label Width="130" Height="25"& Grid.Row="3" Grid.Column="0"& Name="label4"&OneTime&/Label&
&&&&&&& &TextBox Width="150" Height="25"& Grid.Row="3" Grid.Column="1"&& Name="textBox3" Text="{Binding ElementName=scrollBar1, Path=Value,Mode=OneTime}"/&
&&&&&&&& &ScrollBar Value="30" Minimum="0" Grid.RowSpan="4" Grid.Row="0" Grid.Column="2" Maximum="100" Name="scrollBar1" Width="18" Height="{Binding ElementName=GridTable,Path=Height}" /&
&&&&&&& &/Grid&
&/Page&根据程序执行结果,我们可以得到以下结论:
对于OneWay绑定:在界面中显示的数据可以随数据源的值的变化而变化,但更改界面的数据不会影响到数据源。
对于TwoWay绑定:界面中显示的数据及数据源的数据可以双向显示及更新。
对于OneWayToSource绑定:初始时界面的数据为空;更改界面的数据可以影响数据源的值,但是更改数据源的值不会体现在界面上。
对于OneTime绑定:在界面中显示的为数据源的初始值,更改数据源的值的时候,不会更改界面的数据显示;更改界面的数据也不会影响到数据源的数据。
三、绑定目标值影响绑定源值条件
问题:绑定源的值是在您编辑文本的同时进行更新,还是在您结束编辑文本并将鼠标指针从文本框移走后才进行更新呢?或者在您需要更新的情况下在手动的更新呢?
1. UpdateSourceTrigger 属性是确定触发源更新的原因。
下图中右箭头的点演示 UpdateSourceTrigger 属性的角色:
TwoWay及OneWayToSource是由绑定目标到绑定源方向,若实现绑定目标的值更改影响绑定源的值方式,只需要设置相应控件绑定时的UpdateSourceTrigger的值,其值有三种:
PropertyChanged:当绑定目标属性更改时,立即更新绑定源。
LostFocus:当绑定目标元素失去焦点时,更新绑定源。
Explicit:仅在调用 UpdateSource 方法时更新绑定源。
注释:多数依赖项属性的UpdateSourceTrigger 值的默认值为 PropertyChanged,而 Text 属性的默认值为 LostFocus。
代码如下:XAML:
&Page x:Class="WpfDemo.Changed"
&&& xmlns="/winfx/2006/xaml/presentation"
&&& xmlns:x="/winfx/2006/xaml"
&&& Title="Changed"&
&&& &Grid Name="GridTable" Height="250" Background="Silver" Width="350"&
&&&&&&& &Grid.RowDefinitions&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&& &/Grid.RowDefinitions&
&&&&&&& &Grid.ColumnDefinitions&
&&&&&&&&&&& &ColumnDefinition Width="100"&&/ColumnDefinition&
&&&&&&&&&&& &ColumnDefinition Width="150"&&/ColumnDefinition&
&&&&&&&&&&& &ColumnDefinition Width="100"&&/ColumnDefinition&
&&&&&&& &/Grid.ColumnDefinitions&
&&&&&&& &TextBlock Grid.Row="0" Width="90" Height="25" Grid.Column="0" Name="label1" Text="PropertyChanged:"&&/TextBlock&
&&&&&&& &TextBlock Grid.Row="1"& Width="90" Height="25" Grid.Column="0" Name="label2" Text="LostFocus:"&&/TextBlock&
&&&&&&& &TextBlock Grid.Row="2"& Width="90" Height="25" Grid.Column="0" Name="label3" Text="Explicit:"&&/TextBlock&
&&&&&&& &TextBox Grid.Row="0" Width="150" Height="25" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"& Grid.Column="1" Name="TextBox1" /&
&&&&&&& &TextBox Grid.Row="1" Width="150" Height="25" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"&& Grid.Column="1"& Name="TextBox2" /&
&&&&&&& &TextBox Grid.Row="2" Width="150" Height="25" Text="{Binding Path=UserName,Mode=TwoWay,UpdateSourceTrigger=Explicit}"&& Grid.Column="1"& Name="txtExplicit" /&
&&&&&&& &TextBlock Grid.Row="3" Width="90" Height="25"& Grid.Column="0" Name="lblResult" Text="结果:"&&/TextBlock&
&&&&&&& &TextBlock Grid.Row="3" Width="90" Height="25"& Grid.Column="1" Name="lblDisplay" Text="{Binding Path=UserName,Mode=OneWay}"&&/TextBlock&
&&&&&&& &Button Name="btnChanged" Width="90" Height="25" Grid.Row="3" Grid.Column="2"&Explicit&/Button&
&&& &/Grid&
&/Page& 代码如下:C#:
namespace WpfDemo
&&& public partial class Changed : Page
&&&&&&& #region properties
&&&&&&& public UserModel CurrentUser
&&&&&&&&&&&
&&&&&&& #endregion
&&&&&&& #region Constructor
&&&&&&& public Changed()
&&&&&&&&&&& InitializeComponent();
&&&&&&&&&&& this.Loaded += new RoutedEventHandler(Changed_Loaded);
&&&&&&&&&&& this.btnChanged.Click += new RoutedEventHandler(btnChanged_Click);
&&&&&&& #endregion
&&&&&&& #region Changed_Loaded
&&&&&&& void Changed_Loaded(object sender, RoutedEventArgs e)
&&&&&&&&&&& this.CurrentUser = new UserModel() {UserName="swd"};
&&&&&&&&&&& this.DataContext = this.CurrentU
&&&&&&& #endregion
&&&&&&& #region btnLogon_Click
&&&&&&& void btnChanged_Click(object sender, RoutedEventArgs e)
&&&&&&&&& this.txtExplicit.GetBindingExpression(TextBox.TextProperty).UpdateSource();
&&&&&&& #endregion
&&& public class UserModel
&&&&&&& public string UserName
&&&&&&&&&&&}
程序执行结果如上所述。
四、&&&&& 数据提供程序
1. XmlDataProvider:
XmlDataProvider访问 XML 数据的方式有以下三种:
可以使用 XmlDataProvider 类嵌入内联 XML 数据。
可以将 Source 属性设置为 XML 数据文件的 Uri。
可以将 Document 属性设置为 XmlDocument。
注释:当 XmlDocument.NodeChanged 事件发生时,XmlDataProvider 执行所有绑定的完全刷新。 特定节点不进行优化。
默认情况下,XmlDataProvider.IsAsynchronous 属性设置为 true,表示默认情况下 XmlDataProvider 检索数据并异步生成 XML 节点的集合。
以下将介绍使用上面所述的三种方式显示xml数据:
代码如下:Xaml:
&Page x:Class="WpfDemo.xmlBinding"
&&& xmlns="/winfx/2006/xaml/presentation"
&&& xmlns:x="/winfx/2006/xaml"
&&& Title="xmlBinding" xmlns:local="clr-namespace:WpfDemo"&
&&& &Page.Resources&
&&&&&&& &XmlDataProvider x:Key="XmlFile" Source="Students.xml" XPath="/Students"&&/XmlDataProvider&
&&&&&&& &XmlDataProvider x:Key="InnerXmlStu" XPath="/Students"&
&&&&&&&&&&& &x:XData&
&&&&&&&&&&&&&&& &Students xmlns=""&
&&&&&&&&&&&&&&&&&&& &Student&&name&swd&/name&&/Student&
&&&&&&&&&&&&&&&&&&& &Student&&name&awd&/name&&/Student&
&&&&&&&&&&&&&&&&&&& &Student&&name&asd&/name&&/Student&
&&&&&&&&&&&&&&& &/Students&
&&&&&&&&&&& &/x:XData&
&&&&&&& &/XmlDataProvider&
&&& &/Page.Resources&
&&& &Grid&
&&&&&&& &Grid.RowDefinitions&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&&&&&& &RowDefinition&&/RowDefinition&
&&&&&&& &/Grid.RowDefinitions&
&&&&&&& &Grid.ColumnDefinitions&
&&&&&&&&&&& &ColumnDefinition Width="100"&&/ColumnDefinition&
&&&&&&&&&&& &ColumnDefinition Width="150"&&/ColumnDefinition&
&&&&&&& &/Grid.ColumnDefinitions&
&&&&&&& &TextBlock Grid.Row="0" Grid.Column="0"& Height="25" Width="100"& Text="引用XML文件"&&/TextBlock&
&&&&&&& &TextBlock Grid.Row="1" Grid.Column="0" Height="25" Width="100"&& Text="内嵌XML"&&/TextBlock&
&&&&&&& &TextBlock Grid.Row="2" Grid.Column="0"& Height="25" Width="100"& Text="动态XML"&&/TextBlock&
&&&&&&& &ListBox Name="lisbXmlFile" Grid.Row="0" Grid.Column="1" Height="100" Width="150" ItemsSource="{Binding Source={StaticResource XmlFile},XPath=Student/name}"&
&&&&&&& &/ListBox&
&&&&&&& &ListBox Name="lisbInnerXml" Grid.Row="1" Grid.Column="1"& Height="100" Width="150" ItemsSource="{Binding Source={StaticResource InnerXmlStu},XPath=Student/name}"&
&&&&&&& &/ListBox&
&&&&&&& &ListBox Name="lisbXmlDoc" Grid.Row="2" Grid.Column="1"& Height="100"& Width="150" ItemsSource="{Binding XPath=Student/name}"&
&&&&&&& &/ListBox&
&&& &/Grid&
&/Page& 代码如下:XML:
&?xml version="1.0" encoding="utf-8" ?&
&Students&
& &Student&
&&& &name&swd&/name&
&&& &score&110&/score&
& &/Student&
& &Student&
&&& &name&asd&/name&
&&& &score&120&/score&
& &/Student&
& &Student&
&&& &name&awd&/name&
&&& &score&130&/score&
& &/Student&
&/Students&
通过以上示例我想大家应该很容易理解与应用。2. ObjectDataProvider:
ObjectDataProvider 使您能够在 XAML 中创建可用作绑定源的对象,并为您提供以下属性,以对对象执行查询并绑定到结果。
使用 ConstructorParameters 属性将参数传递给对象的构造函数。
使用 MethodName 属性调用一个方法。
使用 MethodParameters 属性将参数传递给该方法。 然后,可以绑定到该方法的结果。
使用ObjectType 指定将提供数据绑定源的对象。
使用 ObjectInstance 属性来指定现有的对象实例作为源
注释:还可以使用 IsAsynchronous 属性指定是在辅助线程还是在活动上下文中执行对象创建。也就是是否异步检索数据。
代码如下:XAML:
namespace WpfDemo
&&& #region CObjectDataProvider
&&& public partial class CObjectDataProvider : Page
&&&&&&& public CObjectDataProvider()
&&&&&&& {InitializeComponent();}
&&& #endregion
&&& #region Country
&&& public class Country
&&&&&&& #region Name
&&&&&&& public string Name
&&&&&&& {}
&&&&&&& #endregion
&&&&&&& #region ProvinceList
&&&&&&& public List&Province& ProvinceList
&&&&&&& {}
&&&&&&& #endregion
&&&&&&& #region GetAllCity
&&&&&&& public static List&Country& GetAllCity()
&&&&&&&&&&& return new List&Country&{
&&&&&&&&&&&& new Country
&&&&&&&&&&&& {
&&&&&&&&&&&&&&&& Name = "中国",
&&&&&&&&&&&&&&&& ProvinceList = new List&Province&
&&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&& new Province{ Name="福建省",
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& CityList=new List&City&{new City{Name="福州市"},new City{Name="厦门市"},new City{Name="漳州市"},new City{Name="泉州市"}}
&&&&&&&&&&&&&&& },
&&&&&&&&&&&&&&& new Province{Name="江苏省",
&&&&&&&&&&&&&&&&&& CityList=new List&City&{
&&&&&&&&&&&&&&&&&& new City{Name="苏州市"},new City{Name="南京市"},new City{Name="扬州市"},new City{Name="无锡市"}}
&&&&&&&&&&&&&&& },
&&&&&&&&&&&&&&& new Province{Name="江西省",
&&&&&&&&&&&&&&&&&&&& CityList=new List&City&{new City{Name="南昌市"},new City{Name="九江市"}}}}
&&&&&&&&&&&& }
&&&&&&&&&&& };
&&&&&&& #endregion
&&& #endregion
#region Province
&&& public class Province
&&&&&&& #region Name
&&&&&&& public string Name
&&&&&&& {}
&&&&&&& #endregion
&&&&&&& #region CityList
&&&&&&& public List&City& CityList
&&&&&&& {}
&&&&&&& #endregion
&&& #endregion
&&& #region City
&&& public class City
&&&&&&& #region Name
&&&&&&& public string Name
&&&&&&& {}
&&&&&&& #endregion
#endregion
五、类型转换与数据校验
1. IValueConverter接口
提供一种将自定义逻辑应用于绑定的方式。
在Binding时,数据源对象到目标对象之间(或者目标对象到数据源对象)可能需要某种转换。这时只需实现IValueConverter接口自定义值转换器即可。
接口原型定义:
public interface IValueConverter{&&& object Convert(object value, Type targetType, object parameter, CultureInfo culture);&&& object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);}
参数value是要转换的值,typeTarget是转换后的值类型,parameter是Binding 类的 ConverterParameter传递过来的参数。
Convert方法:数据绑定引擎在将值从绑定源传播给绑定目标时,调用此方法。
ConvertBack方法:数据绑定引擎在将值从绑定目标传播给绑定源时,调用此方法。
ValueConversion属性作用是告诉自定义转换器类可以转换的源数据和目标数据的 类型(ValueConversion属性将在稍后的示例中看到)。
2. ValidationRule类
提供一种为检查用户输入的有效性而创建自定义规则的方法。
ValidationRule : 所有自定义验证规则的基类。提供了让用户定义验证规则的入口。
ExceptionValidation :表示一个规则,该规则检查在绑定源属性更新过程中引发的异常。它是一个内置的规则,它检查在绑定源属性更新过程中引发的异常。
ValidationResult : 数据验证结果的表现方式。ValidationRule对象的Validate方法执行完毕后通过ValidationResult来表示验证的结果。这里包含了错误信息—ErrorContent,数据是否有效—IsValid。ValidResult 为 ValidationResult 的有效实例。
ValidationError :表示一个验证错误,该错误在 ValidationRule 报告验证错误时由绑定引擎创建。
代码如下:XAML:
&Page x:Class="WpfDemo.TypeConvertAndValidationRule"
&&& xmlns="/winfx/2006/xaml/presentation"
&&& xmlns:x="/winfx/2006/xaml"
&&& Title="TypeConvertAndValidationRule"
&&&&& xmlns:src="clr-namespace:WpfDemo"&
&&& &Grid Height="250" Width="360" Background="Silver"&
&&&&&&& &Grid.RowDefinitions&&&& &RowDefinition&&&& &/RowDefinition&&&& &RowDefinition&&&& &/RowDefinition&&&& &RowDefinition&&&& &/RowDefinition&&&&&&& &/Grid.RowDefinitions&
&&&&&&& &Grid.ColumnDefinitions&&&&&&&&&&&&&&& &ColumnDefinition&&&& &/ColumnDefinition&&&& &ColumnDefinition&&&& &/ColumnDefinition&&&&&&&& &/Grid.ColumnDefinitions&
&&&&&&& &TextBlock Height="25" Width="100" Text="生日"& Grid.Row="0" Grid.Column="0"&&/TextBlock&
&&&&&&& &TextBox Name="txtBirthday" Height="25" Width="150"& Grid.Row="0" Grid.Column="1"&
&&&&&&&&&&& &TextBox.Text&
&&&&&&&&&&&&&&& &Binding Path="Birthday" UpdateSourceTrigger="LostFocus" Mode="TwoWay"&
&&&&&&&&&&&&&&&&&& &Binding.ValidationRules&&src:ValidationDateTimeRule/&&/Binding.ValidationRules&
&&&&&&&&&&&&&&&&&& &Binding.Converter&&src:MyConverterOfBirthFormat/&&/Binding.Converter&
&&&&&&&&&&&&&&& &/Binding&&&&&&&&&&& &/TextBox.Text&
&&&&&&&&&& &TextBox.ToolTip&&&&&&&&&&&&&&&& &Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)&&&&&&& [0].ErrorContent"&&/Binding&&&&&&&&&&& &/TextBox.ToolTip&&&&&&& &/TextBox&
&&&&&&& &TextBlock Height="25" Width="150" Grid.Row="1" Text="{Binding Path=Birthday,Mode=OneWay}" Grid.Column="1"&&/TextBlock&
&&&&&&& &TextBlock Height="25" Width="100" Text="电子邮件格式检查" Grid.Row="2" Grid.Column="0"&&/TextBlock&
&&&&&&& &TextBox Height="25" Width="150" Grid.Row="2" Grid.Column="1"&
&&&&&&&&&&& &TextBox.Text&
&&&&&&&&&&&&&&& &Binding Path="EMail"&
&&&&&&&&&&&&&&&&&&& &Binding.ValidationRules&&ExceptionValidationRule /&&/Binding.ValidationRules&
&&&&&&&&&&&&&&& &/Binding&&&&&&&&&&&& &/TextBox.Text&
&&&&&&&&&&& &TextBox.ToolTip&
&&&&&&&&&&&&&&&&&& &Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"&&/Binding&
&&&&&&&&&&& &/TextBox.ToolTip&&&&&&& &/TextBox&
&&& &/Grid&
&/Page& 代码如下:C#:
namespace WpfDemo
&&& #region TypeConvertAndValidationRule
&&& public partial class TypeConvertAndValidationRule : Page
&&&&&&& public TypeConvertAndValidationRule()
&&&&&&&&&&& InitializeComponent();
&&&&&&&&&&& this.DataContext = new UserInfo { Name = "swd", Birthday =System.Convert.ToDateTime(""), EMail = "" };
&&& #endregion
&&& #region UserInfo
&&& public class UserInfo
&&&&&&& #region Name
&&&&&&& public string Name
&&&&&&& {}
&&&&&&& #endregion
&&&&&&& #region Birthday
&&&&&&& public DateTime Birthday
&&&&&&& {}
&&&&&&& #endregion
&&&&&&& #region EMail
&&&&&&& public string EMail
&&&&&&&&&&& get
&&&&&&&&&&& {}
&&&&&&&&&&& set
&&&&&&&&&&& {
&&&&&&&&&&&&&&& this.email =
&&&&&&&&&&&&&&& Regex r = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
&&&&&&&&&&&&&&& if (!r.IsMatch(value))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&&& throw new ApplicationException("电子邮件格式有误!");
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&&&&& #endregion
&&& #endregion
六、&&&&& 绑定集合对象
1.&&&&&& ICollectionView接口
允许集合具有当前记录管理、自定义排序、筛选和分组这些功能。比如排序,分组,筛选,导航以及其它自定义视图,并且这不会影响到你的后台数据的实际存储。
2.&&&&&& ObservableCollection &T& 类
表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。
3.&&&&&& WPF MVVM概要
MVVM(Model-View-ViewModel)是由MVC,MVP演变而来。MVVM分离了逻辑与界面,解放业务逻辑。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 绑定数据源 的文章

 

随机推荐