资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 definition 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 create 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 available 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 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 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 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 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 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 property 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 FigureFigure 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 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 Validate 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 IDataErrorInfo 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 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 thrown 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号