2010-07-21 125 views
0

我從MSDN複製了一個例子,並添加了Validation.Error事件。問題是,它永遠不會被解僱。爲什麼不?爲什麼在這種情況下不會引發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> 

回答

6

Validation.Error文檔:

當綁定元素運行 成驗證錯誤,但僅用於 綁定設置爲 真 NotifyOnValidationError值時發生。

你只需要設置NotifyOnValidationError在你的綁定:

<Binding 
    Path="Age" 
    Source="{StaticResource data}" 
    ValidatesOnDataErrors="True" 
    UpdateSourceTrigger="PropertyChanged" 
    NotifyOnValidationError="True"> 
</Binding> 
相關問題