为WPF应用程序应用复杂的数据验证规则

上传人:jiups****uk12 文档编号:39998646 上传时间:2018-05-22 格式:DOC 页数:16 大小:298.50KB
返回 下载 相关 举报
为WPF应用程序应用复杂的数据验证规则_第1页
第1页 / 共16页
为WPF应用程序应用复杂的数据验证规则_第2页
第2页 / 共16页
为WPF应用程序应用复杂的数据验证规则_第3页
第3页 / 共16页
为WPF应用程序应用复杂的数据验证规则_第4页
第4页 / 共16页
为WPF应用程序应用复杂的数据验证规则_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《为WPF应用程序应用复杂的数据验证规则》由会员分享,可在线阅读,更多相关《为WPF应用程序应用复杂的数据验证规则(16页珍藏版)》请在金锄头文库上搜索。

1、EnforcingEnforcing ComplexComplex BusinessBusiness DataData RulesRules withwith WPFWPFBrianBrian NoyesNoyesDownloadDownload thethe CodeCode SampleSampleMicrosoft Windows Presentation Foundation (WPF) has a rich data-binding system. In addition to being a key enabler for loose coupling of the UI defi

2、nition from the supporting logic and data through the Model-View-ViewModel (MVVM) pattern, the data-binding system has powerful and flexible support for business data-validation scenarios. The data-binding mechanisms in WPF include several options for evaluating the validity of input data when you c

3、reate an editable view. Plus, WPF templating and styling capabilities for controls give you the ability to easily customize the way you indicate validation errors to the user.To support complex rules and to display validation errors to the user, you generally need to employ a combination of the avai

4、lable validation mechanisms. Even a seemingly simple data input form can present validation challenges when the business rules get complex. Common scenarios involve both simple rules at an individual property level, and cross-coupled properties where the validity of one property depends on the value

5、 of another property. However, the validation support in WPF data binding makes it easy to address these challenges.In this article, youll see how to use the IDataErrorInfo interface implementation, ValidationRules, BindingGroups, exceptions, and validation-related attached properties and events to

6、address your data- validation needs. Youll also see how to customize the display of validation errors with your own ErrorTemplates and ToolTips. For this article, I assume you are already familiar with the basic data- binding capabilities of WPF. For more background on that, see John Papas December

7、2007 MSDN Magazine article, “Data Binding in WPF”.DataData ValidationValidation OverviewOverviewAlmost any time you enter or modify data in an application, you need to ensure that the data is valid before it gets too far away from the source of those changesin this case, the user. Moreover, you need

8、 to give users a clear indication when the data they entered is invalid, and hopefully also give them some indication of how to correct it. These things are fairly easy to do with WPF as long as you know which capability to use and when.When you use data binding in WPF to present business data, you

9、typically use a Binding object to provide a data pipeline between a single property on a target control and a data source object property. For validation to be relevant, youre typically doing TwoWay data bindingmeaning that, in addition to data flowing from the source property into the target proper

10、ty for display, the edited data also flows from target to source as shown in FigureFigure 1 1.Figure 1 DataData FlowFlow in in TwoWayTwoWay DataData BindingBindingThere are three mechanisms for determining whether data entered through a data-bound control is valid. These are summarized in FigureFigu

11、re 2 2.Figure 2 BindingBinding ValidationValidation MechanismsMechanismsValidation MechanismDescriptionExceptionsBy setting the ValidatesOnExceptions property on a Binding object, if an exception is raised in the process of trying to set the modified value on the source object property, a validation

12、 error will be set for that Binding.ValidationRulesThe Binding class has a property to supply a collection of ValidationRule-derived class instances. These ValidationRules need to override a Validate method that will be called by the Binding whenever the data in the bound control changes. If the Val

13、idate method returns an invalid ValidationResult object, a validation error is set for that Binding.IDataErrorInfoBy implementing the IDataErrorInfo interface on a bound data-source object and setting the ValidatesOnDataErrors property on a Binding object, the Binding will make calls to the IDataErr

14、orInfo API exposed from the bound data-source object. If non-null or non- empty strings are returned from those property calls, a validation error is set for that Binding.When a user enters or modifies data in TwoWay data binding, a workflow kicks off:Data is entered or modified by the user through

15、keystrokes, mouse, touch, or pen interaction with the element, resulting in a change of a property on the element.Data is converted to the data-source property type, if needed.The source property value is set.The Binding.SourceUpdated attached event fires.Exceptions are caught by the Binding if thro

16、wn by the setter on the data-source property, and can be used to indicate a validation error.IDataErrorInfo properties are called on the data source object, if implemented.Validation error indications are presented to the user and the Validation.Error attached event fires.As you can see, there are several points in the process where validation errors can result, depending on which mechanism you choose. Not shown in the list is w

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号