1 / 16

在本单元中,您将学习: 执行数据转换和验证

目标. 在本单元中,您将学习: 执行数据转换和验证. 处理 WPF 中的验证. .NET Framework 提供各种验证规则来确保用户输入了正确数据。 验证检验可以基于类型、范围、格式或其他特定于应用程序的需求。 在 WinForms 中, ErrorProvider 控件用于向控件应用验证。. 在 WPF 中,使用 ValidationRules 对象应用验证。 WPF 包含以下类型的内置 ValidationRule 对象: ExceptionValidationRule 对象:如果在更新源绑定属性时抛出任何异常,那么认为用户输入的值无效。

Download Presentation

在本单元中,您将学习: 执行数据转换和验证

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 目标 • 在本单元中,您将学习: • 执行数据转换和验证

  2. 处理 WPF 中的验证 • .NET Framework 提供各种验证规则来确保用户输入了正确数据。 • 验证检验可以基于类型、范围、格式或其他特定于应用程序的需求。 • 在 WinForms 中,ErrorProvider 控件用于向控件应用验证。

  3. 在 WPF 中,使用 ValidationRules 对象应用验证。 WPF 包含以下类型的内置 ValidationRule 对象: ExceptionValidationRule 对象:如果在更新源绑定属性时抛出任何异常,那么认为用户输入的值无效。 DataErrorValidationRule 对象:验证实现 IDataErrorInfo 接口的对象所引发的错误。 IDataErrorInfo 接口允许您将错误信息关联到 UI 控件。 ValidationRule 对象检查实现 IDataErrorInfo 的源对象是否生成了错误。 处理 WPF 中的验证(续)

  4. 可使用以下属性提供关于应用程序 UI 上数据无效的一些反馈: Validation.ErrorTemplate:可设置为自定义控件,以显示错误消息。 ToolTip:可设置为显示错误消息。 处理 WPF 中的验证(续)

  5. 以下代码段定义实现IDataErrorInfo接口的Employee 类: private intempno; public intEmpNo {get{return empno;} set {empno = value;}} public string Error { get { return null; } } public string this[string eno] {   get {string res = null; if (eno == "EmpNo") { if (this.empno < 0) { res = "Emp No should not be less than 0";}} return res;}} 处理 WPF 中的验证(续)

  6. 以下代码段定义实现IDataErrorInfo接口的Employee 类: private intempno; public intEmpNo {get{return empno;} set {empno = value;}} public string Error { get { return null; } } public string this[string eno] { get {string res = null; if (eno == "EmpNo") { if (this.empno < 0) { res = "Emp No should not be less than 0";}} return res;}} 处理 WPF 中的验证(续) Error 属性获取 EmpNo 属性的值并验证它是否小于 0。如果是,它返回包含错误消息的 res 字符串。

  7. 以下代码段在 TextBox 控件上应用验证规则: <Window.Resources> <src:Employee x:Key="Emp"></src:Employee> <Style x:Key="txterror" TargetType="{x:Type TextBox}"><Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"> </Setter> 处理 WPF 中的验证(续)

  8. 以下代码段在 TextBox 控件上应用验证规则: <Window.Resources> <src:Employee x:Key="Emp"></src:Employee> <Style x:Key="txterror" TargetType="{x:Type TextBox}"><Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"> </Setter> 处理 WPF 中的验证(续) 如果生成错误,将激活触发器,工具提示将显示错误消息。

  9. <Setter Property="Background“ Value="Red"> </Setter></Trigger></Style.Triggers> </Style> </Window.Resources> <Grid> <Label Name="label1" VerticalAlignment="Top" Width="151">Emp.No</Label> <TextBox Name="txteno" Style="{StaticResourcetxterror}"> <TextBox.Text> <Binding Path="EmpNo" Source="{StaticResourceEmp}" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged"> 处理 WPF 中的验证(续)

  10. <Binding.ValidationRules> <ExceptionValidationRule> </ExceptionValidationRule> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </Grid> 处理 WPF 中的验证(续)

  11. <Binding.ValidationRules> <ExceptionValidationRule> </ExceptionValidationRule> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </Grid> 处理 WPF 中的验证(续) 检查源对象是否引发错误并将错误显示在工具提示中。

  12. 下图显示了上述代码段的输出。 处理 WPF 中的验证(续)

  13. 验证过程通常发生在TwoWay 和 OneWayToSource数据绑定模式中。 以下步骤解释了TwoWay 或 OneWayToSource绑定模式中的验证过程: 绑定引擎检查是否有任何自定义ValidationRule对象。如果有,它对每个ValidationRule对象调用Validate方法。 如果验证规则没有通过,那么绑定引擎创建ValidationError对象并将它添加到绑定元素的Validation.Errors集合。 处理 WPF 中的验证(续)

  14. 问题陈述: 检查了 John 创建的 UI 后,项目经理要他临时制作该模块。他需要验证用户输入的数据并在输入不正确的情况下提供相关信息。 John 要在以下字段上实现验证。 电影:此字段不应留空。  演员:此字段不应留空,并且值不能为数字。  导演:此字段不应留空,并且值不能为数字。  他要实现这些修改,然后发送给项目经理验证。 活动 3:验证数据

  15. 解决方案: 为了在VideoLibrary应用程序中实现验证,需要执行以下任务: 打开VideoLibrary项目。 设计应用程序的用户界面。 向设计添加功能。 执行应用程序并验证输出。 活动 3:验证数据(续)

  16. 在本单元中,您学习了: WPF 使用 ValidationRule 对象应用验证。WPF 中提供了以下内置 ValidationRule 对象: ExceptionValidationRule 对象 DataErrorValidationRule 对象 可使用以下属性提供验证过程的可视化反馈信息: Validation.ErrorTemplate ToolTip 小结

More Related