Binding.ValidatesOnExceptions 속성

정의

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

속성 값

Boolean

ExceptionValidationRule을 포함하면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 .에서 TextBox사용자 입력의 유효성을 검사하는 데 사용합니다ValidatesOnExceptions. 첫 번째 예제에서는 속성이 잘못된 속성으로 설정된 경우 예외를 Age throw하는 데이터 형식을 만듭니다.

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

다음 예제에서는 속성을 바인딩하고 에 설정합니다 ValidatesOnExceptions true Binding.TextBox Age 사용자가 잘못된 값을 입력하면 빨간색 테두리가 TextBox 표시되고 ToolTip 오류 메시지가 보고됩니다.

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

설명

이 속성을 설정하면 요소를 명시적으로 사용하는 대신 사용할 ExceptionValidationRule 수 있습니다. 원본 ExceptionValidationRule 속성을 업데이트하는 동안 throw되는 예외를 확인하는 기본 제공 유효성 검사 규칙입니다. 예외가 throw되면 바인딩 엔진은 예외를 ValidationError 만들어 바인딩된 요소의 컬렉션에 Validation.Errors 추가합니다. 오류가 없으므로 다른 규칙이 유효성 검사 문제를 제기하지 않는 한 이 유효성 검사 피드백이 지워지게 됩니다.

ValidatesOnExceptions .NET Framework 버전 3.5에서에서 도입 되었습니다. 자세한 내용은 .NET Framework 버전 및 종속성을 참조하십시오.

적용 대상

추가 정보