Binding.ValidatesOnDataErrors Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy należy dołączyć element 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

Wartość właściwości

true w celu uwzględnienia elementu DataErrorValidationRule; w przeciwnym razie false.

Przykłady

W poniższych przykładach użyto IDataErrorInfo metody i ValidatesOnDataErrors zweryfikować dane wejściowe użytkownika w elemecie TextBox. Pierwszy przykład tworzy typ danych implementujący zgłaszanie IDataErrorInfo błędów walidacji.

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

W poniższym przykładzie właściwość jest powiązana z Age właściwością TextBox i ustawioną ValidatesOnDataErrors na true .Binding Gdy użytkownik wprowadzi nieprawidłową wartość, w obiekcie TextBox pojawi się czerwone obramowanie i ToolTip zgłasza komunikat o błędzie.

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

Uwagi

Ustawienie tej właściwości stanowi alternatywę dla jawnego DataErrorValidationRule użycia elementu. Jest DataErrorValidationRule to wbudowana reguła sprawdzania poprawności, która sprawdza błędy, które są wywoływane przez implementację IDataErrorInfo obiektu źródłowego. Jeśli zostanie zgłoszony błąd, aparat powiązania tworzy ValidationError element z błędem i dodaje go do Validation.Errors kolekcji powiązanego elementu. Brak błędu czyści tę opinię weryfikacyjną, chyba że inna reguła zgłasza problem z walidacją.

ValidatesOnDataErrorsjest wprowadzana w wersji .NET Framework 3.5. Aby uzyskać więcej informacji, zobacz .NET Framework Wersje i zależności.

Dotyczy

Zobacz też