2013-08-06 2 views
1

저는 WPF에서 초보자입니다. 이제는 유효성 검사에 어려움을 겪고 있습니다. Validation 메서드의 Validation 클래스에서 TextBox1의 값과 TextBox2의 값을 비교해야합니다. 두 가지 모두 내 양식에 포함되어 있습니다.WPF ValidationRule - 두 값을 서로 비교하는 방법

WPF에서 그렇게 할 수있는 방법이 있습니까? this- Xaml.cs에 대해 파일을 어떻게

+0

유효성 검사가 수행되고 싶지 ..? 두 개의 텍스트 상자 값을 모두 입력하면 버튼 클릭 이벤트가 발생합니다. ?? – Vishal

+0

Textbox textchanged에서 처리해야합니다. –

+0

당신은 그것을 어떻게 원하게 만들 수 있습니까? – Vishal

답변

1

private string _Txt1; 

      public string Txt1 
      { 
       get { return _Txt1; } 
       set { _Txt1 = value; 
       OnPropertyChanged("Txt1"); 
       } 
      } 

      private string _Txt2; 

      public string Txt2 
      { 
       get { return _Txt2; } 
       set 
       { 
        _Txt2 = value; 
        OnPropertyChanged("Txt2"); 
       } 
      } 

    public event PropertyChangedEventHandler PropertyChanged; 

      /// <summary> 
      /// Called when [property changed]. 
      /// </summary> 
      /// <param name="PropertyName">Name of the property.</param> 
      private void OnPropertyChanged(string PropertyName) 
      { 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
       } 
      } 

private void textbox2_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      Check(); 
     } 

     private void textbox1_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      Check(); 
     } 

     public void Check() 
     { 
      if (Txt1 == Txt2) 
      { 
       MessageBox.Show("Values cant be same"); 
       Txt1 = ""; 
       Txt2 = ""; 
      } 
     } 

XAML 파일 -

<Grid> 
     <TextBox Name="textbox1" Width="100" Height="20" TextChanged="textbox1_TextChanged" Text="{Binding Txt1,RelativeSource={RelativeSource AncestorType=Window},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
     <TextBox Name="textbox2" Width="100" Height="20" Margin="81,146,322,146" TextChanged="textbox2_TextChanged" Text="{Binding Txt2,RelativeSource={RelativeSource AncestorType=Window},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 
    </Grid> 
관련 문제