如果我的数据绑定失败验证,我该如何获得原始值 [英] How can i get original value if my databinding fails validation
问题描述
ItemsSource = {Binding SomeCollection,Mode = TwoWay}
。此设置工作正常,我可以读取值并从UI更新它们。 稍后我加入了转换器来验证值。验证失败时显示为空。现在我有兴趣得到原始的值,以防止验证失败。
我在这里有什么选择?
我从来没有使用转换器进行验证。相反,我使用实现IDataErrorInfo的项目,其中的数据绑定属性为ValidatesOnDataErrors = True。
使用此验证方法,保留原始值,对象返回错误值(在我的情况下是一个字符串说什么是错误)。我的视图的控件有一个自定义的验证项目,增加了一个随着时间的推移而变淡的红色边框,以及一个在鼠标悬停上弹出的工具提示。
然后你只需要把您正在显示的数据类中的验证规则:
Private Sub OnAddress1Changed()
Me.RemoveError(Address1 )
如果_Address1是Nothing OrElse _Address1.Trim =然后
Me.AddError(Address1,请输入有效的地址行)
如果
OnPropertyChanged( CanShip)
End Sub
私有m_validationErrors作为新字典(String,String)
Private Sub AddError(ByVal ColName As String,ByVal Msg As String )
如果不是m_validationErrors.ContainsKey(ColName)然后
m_validationErrors.Add(ColName,Msg)
End If
End Sub
Private Sub RemoveError(ByVal ColName As String )
如果m_validationErrors.ContainsKey(ColName)然后
m_validationErrors.Remove(ColName)
End If
End Sub
公共ReadOnly属性[错误]()As String实现System.ComponentModel.IDataErrorInfo.Error
获取
如果m_validationErrors.Count> 0然后
返回装运数据无效
Else
返回没有
结束如果
结束获取
结束属性
默认公共只读属性项(ByVal columnName As String)As String Implements System.ComponentModel.IDataErrorInfo.Item
Get
如果m_validationErrors.ContainsKey(columnName)然后
返回m_validationErrors(columnName).ToString
Else
返回没有
结束如果
结束获取
结束属性
编辑
只是为了表现出来,我将给出一个示例验证模板来显示其他如何挂接它。 p>
< Style x:Key =ToolTipValidationTargetType ={x:Type Control}>
< Setter Property =Validation.ErrorTemplate>
< Setter.Value>
< ControlTemplate>
< Border BorderBrush =RedBorderThickness =2,1,2,1>
< AdornedElementPlaceholder />
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Style.Triggers>
< Trigger Property =Validation.HasErrorValue =True>
< Setter Property =ToolTipValue ={Binding(Validation.Errors)[0] .ErrorContent,RelativeSource = {x:Static RelativeSource.Self}}/>
< / Trigger>
< /Style.Triggers>
< / Style>
一个视频可以随身携带。 视频号码4
I have a working WPF dialog that uses DataGrid. The DataGrid is set to have ItemsSource={Binding SomeCollection, Mode=TwoWay}
. This setup works fine, I can read values and update them from UI.
Later I have added Converters to validate values. I show blank when validation fails. Now I am interested in getting the original values back in case if validation fails.
What options do i have here?
I have never used a converter for validation. Instead I use items that implement IDataErrorInfo with an attribute in the databinding attribute for ValidatesOnDataErrors=True.
Using this method of validation, the original value is retained, and the object returns the errors value (in my case a string saying what the error is). My View's controls have a custom validation item which adds a red border which fades over time, and a tooltip that pops up on mouse hover.
Then you just need to put your validation rules in the data classes you are displaying:
Private Sub OnAddress1Changed()
Me.RemoveError("Address1")
If _Address1 Is Nothing OrElse _Address1.Trim = "" Then
Me.AddError("Address1", "Please enter a valid Address Line")
End If
OnPropertyChanged("CanShip")
End Sub
Private m_validationErrors As New Dictionary(Of String, String)
Private Sub AddError(ByVal ColName As String, ByVal Msg As String)
If Not m_validationErrors.ContainsKey(ColName) Then
m_validationErrors.Add(ColName, Msg)
End If
End Sub
Private Sub RemoveError(ByVal ColName As String)
If m_validationErrors.ContainsKey(ColName) Then
m_validationErrors.Remove(ColName)
End If
End Sub
Public ReadOnly Property [Error]() As String Implements System.ComponentModel.IDataErrorInfo.Error
Get
If m_validationErrors.Count > 0 Then
Return "Shipment data is invalid"
Else
Return Nothing
End If
End Get
End Property
Default Public ReadOnly Property Item(ByVal columnName As String) As String Implements System.ComponentModel.IDataErrorInfo.Item
Get
If m_validationErrors.ContainsKey(columnName) Then
Return m_validationErrors(columnName).ToString
Else
Return Nothing
End If
End Get
End Property
Edit
And just for the heck of it, I will put an example validation template in to show others how to hook it up.
<Style x:Key="ToolTipValidation" TargetType="{x:Type Control}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="2,1,2,1">
<AdornedElementPlaceholder/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
Finally: A MSDN article on implementing Validation
A video to go with it. Video number 4
这篇关于如果我的数据绑定失败验证,我该如何获得原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!