Binding.ValidatesOnDataErrors 属性

定义

获取或设置一个值,该值指示是否包含 DataErrorValidationRule

public:
 property bool ValidatesOnDataErrors { bool get(); void set(bool value); };
public bool ValidatesOnDataErrors { get; set; }
member this.ValidatesOnDataErrors : bool with get, set
Public Property ValidatesOnDataErrors As Boolean

属性值

如果包含 DataErrorValidationRule,则为 true;否则为 false

示例

以下示例使用 IDataErrorInfoValidatesOnDataErrors 来验证 中的 TextBox用户输入。 第一个示例创建实现以报告验证错误的数据类型 IDataErrorInfo

public class PersonImplementsIDataErrorInfo : IDataErrorInfo
{
    private int age;

    public int Age
    {
        get { return age; }
        set { age = value; }
    }

    public string Error
    {
        get
        {
            return "";
        }
    }

    public string this[string name]
    {
        get
        {
            string result = null;

            if (name == "Age")
            {
                if (this.age < 0 || this.age > 150)
                {
                    result = "Age must not be less than 0 or greater than 150.";
                }
            }
            return result;
        }
    }
}
Public Class PersonImplementsIDataErrorInfo
    Implements System.ComponentModel.IDataErrorInfo
    Private m_age As Integer

    Public Property Age() As Integer
        Get
            Return m_age
        End Get
        Set(ByVal value As Integer)
            m_age = value
        End Set
    End Property

    Public ReadOnly Property [Error]() As String _
                    Implements System.ComponentModel.IDataErrorInfo.Error
        Get
            Return ""
        End Get
    End Property

    Default Public ReadOnly Property Item(ByVal name As String) As String _
                            Implements System.ComponentModel.IDataErrorInfo.Item
        Get
            Dim result As String = Nothing

            If name = "Age" Then
                If Me.m_age < 0 OrElse Me.m_age > 150 Then
                    result = "Age must not be less than 0 or greater than 150."
                End If
            End If
            Return result
        End Get
    End Property
End Class

以下示例将 Age 属性绑定到 ,并将 在 上Binding设置为 ValidatesOnDataErrorstrueTextBox 当用户输入无效值时,会在 中 TextBox 显示一个红色边框, ToolTip 并报告错误消息。

<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}">
    <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>

注解

设置此属性提供了显式使用 元素的 DataErrorValidationRule 替代方法。 DataErrorValidationRule是一个内置验证规则,用于检查源对象的实现引发IDataErrorInfo的错误。 如果引发错误,绑定引擎将 ValidationError 创建包含错误的 ,并将其添加到 Validation.Errors 绑定元素的集合中。 缺少错误会清除此验证反馈,除非其他规则引发验证问题。

ValidatesOnDataErrors.NET Framework版本 3.5 中引入了 。 有关详细信息,请参见 .NET Framework 版本和依赖关系

适用于

另请参阅