2013-08-06 25 views
1

我是WPF新手,現在我正在努力與其驗證。 在我的驗證類的驗證方法中,我需要比較TextBox1的值和TextBox2的都包含在我的表單中。WPF驗證規則 - 如何比較兩個值彼此

在WPF中有沒有辦法做到這一點?

+0

..?當兩個文本框的值都被給出時,按鈕點擊事件。 ?? – Vishal

+0

我需要它在Textbox textchanged上完成。 –

+0

你能舉個例子嗎? – Vishal

回答

1

如何這 - Xaml.cs文件

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> 
當你想要做驗證