- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
关于 MSDN Magazine它有一篇关于 MVVM 的好文章,他们将 Xaml 中的验证错误绑定(bind)到 Validation.ErrorTemplate="{x:Null}"
。我不明白为什么以及如何从 IDataErrorInfo 中显示错误?谁能告诉我如何使用 MVVM 方法将错误消息显示在屏幕上?
最佳答案
绑定(bind)到支持 IDataErrorInfo 的对象时,需要考虑 WPF 绑定(bind)类的几个功能:
ValidatesOnDataErrors 必须为真。这指示 WPF 在基础对象上查找和使用 IDataError 接口(interface)。
如果源对象的 IDataError 接口(interface)报告验证问题,附加属性 Validation.HasError 将在目标对象上设置为 true。然后,您可以将此属性与触发器一起使用,以更改控件的工具提示以显示验证错误消息(我在当前项目中这样做,最终用户很喜欢)。
Validation.Errors 附加属性将包含上次验证尝试导致的任何 ValidationResult 错误的枚举。如果您要使用工具提示方法,请使用 IValueConverter 仅检索第一个项目...否则您会遇到显示错误消息本身的绑定(bind)错误。
绑定(bind)类公开 NotifyOnValidationError,当它为 True 时,每当验证规则的状态发生变化时,都会导致路由事件从绑定(bind)控件冒泡。如果您想在绑定(bind)控件的容器中实现事件处理程序,然后向列表框添加验证消息或从列表框删除验证消息,这将非常有用。
MSDN 上有用于执行两种反馈方式(工具提示和列表框)的示例,但我将粘贴到我负责在我的 DataGridCells 和 TextBox 上实现工具提示反馈的代码下方...
DataGridCell 样式:
<Style TargetType="{x:Type dg:DataGridCell}"
x:Key="DataGridCellStyle">
<Setter Property="ToolTip"
Value="{Binding Path=Column.(ToolTipService.ToolTip),RelativeSource={RelativeSource Self}}" />
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors), Converter={StaticResource ErrorContentConverter}}" />
</Trigger>
</Style.Triggers>
</Style>
文本框样式:
<Style x:Key="ValidatableTextBoxStyle" TargetType="TextBox">
<!--When the control is not in error, set the tooltip to match the AutomationProperties.HelpText attached property-->
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=(AutomationProperties.HelpText)}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
ErrorContentConverter(用于检索工具提示的第一条验证错误消息):
Imports System.Collections.ObjectModel
Namespace Converters
<ValueConversion(GetType(ReadOnlyObservableCollection(Of ValidationError)), GetType(String))> _
Public Class ErrorContentConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim errors As ReadOnlyObservableCollection(Of ValidationError) = TryCast(value, ReadOnlyObservableCollection(Of ValidationError))
If errors IsNot Nothing Then
If errors.Count > 0 Then
Return errors(0).ErrorContent
End If
End If
Return String.Empty
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
End Namespace
...最后是在文本框中使用样式的示例:
<TextBox Text="{Binding Path=EstimatedUnits,ValidatesOnDataErrors=True,NotifyOnValidationError=True}"
Style="{StaticResource ValidatableTextBoxStyle}"
AutomationProperties.HelpText="The number of units which are likely to sell in 1 year." />
关于c# - MVVM 模式、IDataErrorInfo 和绑定(bind)显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1123062/
我正在使用 MVVM 在 WPF 项目中工作. 使用 IDataErrorInfo 验证错误的最佳做法是什么? ?在我的模型或 View 模型中? 用于实现验证的最佳模式是什么? 附注我正在使用 .N
我正在创建一个将使用 IDataErrorInfo 的 WPF 应用程序我的业务对象实现的数据验证。我一直在使用 this demo from a blog post了解ValidatesOnData
注意-我的类(class)是EntityObject上课! 我有以下类(class): public class Foo { public Bar Bar { get; set; } } pu
在 MVVM IDataErrorInfo用于验证。在这个时间索引器检查值不能得到。在调用它后如何检查我无法获取的值。代码在这里。列名中传递的值是什么。 public string this[stri
我有一个简单定义如下的地址对象: public class Address { public string StreetNumber { get; set; } public stri
自从我尝试学习 WPF 以来,我越来越多地看到使用接口(interface) IDataErrorInfo 将错误绑定(bind)到接口(interface)。我的问题是我通常将数据验证放在属性的 s
我不知道是我找不到它还是它不存在,但是是否有任何验证属性可以检查集合是否为空/空? 如果没有,是否有关于如何创建我自己的验证属性的好资源? 最佳答案 您好,当您按照默认使用 DataAnnotatio
昨天,我问了一个关于 MVVM 验证的问题,有人用一段代码回答:https://stackoverflow.com/a/13387724/ 我试图理解这段代码,但我就是不理解索引器部分。有人可以向我解
我有一个绑定(bind)到 winform 的对象,这个对象实现了 IDataErrorInfo。我有一个错误提供者。问题是当一个属性的一个属性发生变化时。 当我更改年龄时没有问题(即检查并正确显示/
好吧,现在我正在验证数据。我有这段代码: 在我看来:
我在使用 IDataErrorInfo 验证 ComboBox 时遇到问题。 我设置了 1 个文本框和 1 个组合框,在运行程序时,第一个焦点在文本框上,当我点击 Tab 以聚焦在组合框中时,我得到:
这个问题可能很简单,但是我找不到办法做,所以我问了。 我在我的模型中使用 IDataErrorInfo 来验证它如下: public class Group : INotifyPropertyChan
我正在为 WPF 应用程序编写(尝试编写)单元测试。 UI 绑定(bind)以实现 IDataErrorInfo 的业务对象,这样当我在 View xaml 中设置 ValidatesOnDataEr
DataAnnotations 与 IDataErrorInfo 两者的优点和缺点? 一个比另一个的好处? (尤其是与 MVC 相关的) 最佳答案 因为我不想开始一个新问题,所以迟到了讨论。我的出发点
目前我的业务对象实现IDataErrorInfo .由于我打算在 ASP.NET MVC 3 中使用这些库,我认为我应该实现 IValidatableObject也可以代替。 WPF 是否与 IVal
我在某些面板上有两个控件:文本框和组合框: 当我在组合框上选择某些内容时,我需要强制对文本框进行 idataerrorinfo 验证。怎么做? View 模型中的代码: string IDataE
我们在我的 MVVM 应用程序中有一个配置设置页面。此处的字段之一(属性:BackupFolderPath)有一个 TextBox 控件。我们已经使用 IDataErrorInfo 对该控件进行了验证
我是 WPF MVVM 的新手,想对本文提出一个后续问题: Enable Disable save button during Validation using IDataErrorInfo 如果表单
我有 ItemType ,它在 IDataErrorInfo 接口(interface)的帮助下实现了验证所需的一切: #region IDataErrorInfo implementation
我有一个实现 IDataError 的 View 模型。在 View 模型中,我有一个 ObservableCollection。 ObservableCollection 在我的 View 中填充了
我是一名优秀的程序员,十分优秀!