我从MSDN复制了一个示例并添加了Validation.Error事件。问题是,它永远不会被解雇。为什么不呢?
<UserControl x:Class="MeasurementControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:src="clr-namespace:Controls"
mc:Ignorable="d"
d:DesignWidth="300">
<StackPanel Margin="20">
<StackPanel.Resources>
<src:PersonImplementsIDataErrorInfo x:Key="data"/>
<!--The tool tip for the TextBox to display the validation error message.-->
<Style x:Key="textBoxInError" TargetType="TextBox">
<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>
</StackPanel.Resources>
<TextBlock>Enter your age:</TextBlock>
<TextBox Style="{StaticResource textBoxInError}" Validation.Error="TextBoxScalar_Error">
<TextBox.Text>
<!--ValidatesOnDataErrors to is set to True, so the Binding
checks for errors raised by the IDataErrorInfo object.
An alternative syntax is to add <DataErrorValidationRule/> within
the <Binding.ValidationRules> section.-->
<Binding
Path="Age"
Source="{StaticResource data}"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged">
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock>Mouse-over to see the validation error message.</TextBlock>
</StackPanel>
答案 0 :(得分:6)
来自Validation.Error的文档:
绑定元素运行时发生 进入验证错误,但仅限于 绑定与 NotifyOnValidationError值设置为 真。
您只需在绑定中设置NotifyOnValidationError:
<Binding
Path="Age"
Source="{StaticResource data}"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"
NotifyOnValidationError="True">
</Binding>