Mar*_*ark 16
绑定到支持IDataErrorInfo的对象时,需要考虑WPF Binding类的几个功能:
ValidatesOnDataErrors必须为True.这指示WPF在底层对象上查找并使用IDataError接口.
如果源对象的IDataError接口报告验证问题,则附加属性Validation.HasError将在目标对象上设置为true.然后,您可以使用此属性和触发器来更改控件的工具提示以显示验证错误消息(我在当前项目中执行此操作,最终用户喜欢它).
Validation.Errors附加属性将包含上次验证尝试产生的任何ValidationResult错误的枚举.如果您使用工具提示方法,请使用IValueConverter仅检索第一个项目...否则您将遇到绑定错误以显示错误消息本身.
绑定类公开NotifyOnValidationError,当为True时,每次验证规则的状态发生更改时,都会导致路由事件从绑定控件冒泡.如果要在绑定控件的容器中实现事件处理程序,然后在列表框中添加和删除验证消息,这将非常有用.
MSDN上有两个样本用于执行两种反馈(工具提示以及列表框),但我会粘贴下面的代码,以实现我的DataGridCells和TextBoxes上的工具提示反馈......
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>
Run Code Online (Sandbox Code Playgroud)
TextBox样式:
<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>
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
...最后是在文本框中使用样式的示例:
<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." />
Run Code Online (Sandbox Code Playgroud)
几分钟前我正在查看相同的样本.你的猜测是正确的.在此代码示例中,他们从TextBox控件中删除了默认的ErrorTemplate,因此它不会显示红色矩形.他们不是使用ErrorTemplate而是创建ContentProvider,其内容绑定到特定文本框的验证错误.
| 归档时间: |
|
| 查看次数: |
10736 次 |
| 最近记录: |