Binding.ValidatesOnExceptions Egenskap

Definition

Hämtar eller anger ett värde som anger om .ExceptionValidationRule

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

Egenskapsvärde

true för att inkludera ExceptionValidationRule; annars false.

Exempel

I följande exempel används ValidatesOnExceptions för att verifiera användarindata i en TextBox. I det första exemplet skapas en datatyp som utlöser ett undantag när egenskapen Age är inställd på en ogiltig egenskap.

public class PersonThrowException
{
    private int age;

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

            if (value < 0 || value > 150)
            {
                throw new ArgumentException("Age must not be less than 0 or greater than 150.");
            }
            age = value;
        }
    }
}
Public Class PersonThrowException
    Private m_age As Integer

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

            If value < 0 OrElse value > 150 Then
                Throw New ArgumentException("Age must not be less than 0 or greater than 150.")
            End If
            m_age = value
        End Set
    End Property
End Class

I följande exempel binder Age egenskapen till TextBox och anges ValidatesOnExceptions till trueBinding. När användaren anger ett ogiltigt värde visas en röd kantlinje i TextBox och ToolTip rapporterar felmeddelandet.

<StackPanel Margin="20">
  <StackPanel.Resources>
    
    <src:PersonThrowException 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>
      <!--By setting ValidatesOnExceptions to True, it checks for exceptions
        that are thrown during the update of the source property.
        An alternative syntax is to add <ExceptionValidationRule/> within
        the <Binding.ValidationRules> section.-->
      <Binding Path="Age" Source="{StaticResource data}"
               ValidatesOnExceptions="True"
               UpdateSourceTrigger="PropertyChanged">
      </Binding>
    </TextBox.Text>
  </TextBox>
  <TextBlock>Mouse-over to see the validation error message.</TextBlock>
</StackPanel>

Kommentarer

Att ange den här egenskapen är ett alternativ till att använda elementet ExceptionValidationRule explicit. ExceptionValidationRule är en inbyggd verifieringsregel som söker efter undantag som genereras under uppdateringen av källegenskapen. Om ett undantag utlöses skapar bindningsmotorn ett ValidationError med undantaget och lägger till det i Validation.Errors samlingen av det bundna elementet. Avsaknaden av ett fel rensar den här valideringsfeedback, såvida inte en annan regel genererar ett valideringsproblem.

ValidatesOnExceptions introduceras i .NET Framework version 3.5. Mer information finns i .NET Framework-versioner och beroenden.

Gäller för

Se även