2013-03-05 67 views
1

我的視圖模型WPF結合工作不正常

public class MyViewModel : ViewModelBase 
     { 
      // INotifyPropertyChanged is implemented in ViewModelBase 

      private String _propX; 
      public String PropX 
      { 
       get { return _propX; } 
       set 
       { 
        if (_propX != value) 
        { 
         _propX = value; 
         RaisePropertyChanged(() => PropX); 
        } 
       } 
      } 

      private String _propY; 
      public String ServerIP 
      { 
       get { return _propY; } 
       set 
       { 
        if (_propY != value) 
        { 
         _propY = value; 
         RaisePropertyChanged(() => ServerIP); 
        } 
       } 
      } 

      public A() 
      { 
       this._propY = "000.000.000.000"; 
       this._propY = "000.000.000.000"; 
      } 
     } 

// EDIT 
// This is the command that resets the properties 
    private RelayCommand _resetFormCommand; 
    public ICommand ResetConnectionFormCommand 
    { 
     get 
     { 
      if (_resetFormCommand == null) 
      { 
       _resetFormCommand = new RelayCommand(param => this.ExecuteResetFormCommand(), param => this.CanExecuteResetFormCommand); 
      } 

      return _resetFormCommand; 
     } 
    } 

    private bool CanExecuteResetFormCommand 
    { 
     get 
     { 
      return !String.IsNullOrWhiteSpace(this._propX) || 
       !String.IsNullOrWhiteSpace(this._propY); 
     } 
    } 

    private void ExecuteResetFormCommand() 
    { 
     this._propX = ""; 
     this._propY = ""; 
    } 

我查看XAML

<TextBox Name="propX" Text="{Binding PropX }" PreviewTextInput="textBox_PreviewTextInput" /> 
<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput" /> 
<Border> 
    <Button Content="Reset" Name="resetBtn" Command="{Binding ResetFormCommand}" /> 
</Border> 

我查看後面

private MyViewModel vm; 
public ConnectionUserControl() 
{ 
    InitializeComponent(); 
    vm = new MyViewModel(); 
    this.DataContext = vm; 
} 

private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    ValidateInput(sender as TextBox, e); 
} 

reset命令在我的視圖模型重置屬性代碼,但該文本框仍然包含它們的值,綁定不能正常工作:( 我是mi在這裏點東西?

+0

你能否顯示你的復位命令的動作代碼? – ethicallogics 2013-03-05 12:25:30

+0

是的,我編輯了視圖模型類 – Stacked 2013-03-05 13:39:41

回答

3

,應重置屬性,不私人會員:

private void ExecuteResetFormCommand() { this.PropX =「」; this.PropY =「」; }

+0

工程就像一個魅力。你能告訴我爲什麼this._propX不起作用,並且this.PropX工作正常嗎?謝謝 – Stacked 2013-03-05 13:45:49

+0

請注意,您在獲取屬性 – user1064519 2013-03-05 13:51:53

+0

RaisePropertyChanged(()=> PropX/* not _propX * /)的屬性時引發了PropertyChanged事件;我知道了:)謝謝 – Stacked 2013-03-05 13:57:43

0

你是如何重置數值的?重置值時,您可能會重寫數據綁定。如果您發佈單擊該按鈕時執行的代碼,這將會很有幫助。

0

在你的XAML代碼,你必須設置喜歡的結合:

<TextBox Name="propX" Text="{Binding PropX, Mode=TwoWay}" .../> 
+0

用戶可編輯的屬性,例如TextBox.Text,ComboBox.Text,MenuItem.IsChecked等,具有TwoWay作爲它們的默認模式值 – ethicallogics 2013-03-05 12:53:44

0

結合必須是雙向的,以便文本框將自己從視圖模型更新

+0

用戶可編輯的屬性,如TextBox.Text, ComboBox.Text,MenuItem.IsChecked等,有TwoWay作爲它們的默認模式值 – ethicallogics 2013-03-05 12:53:08

+0

這也是我的想法,爲什麼我沒有將模式屬性添加到綁定。 – Stacked 2013-03-05 13:41:53

0

在您的代碼隱藏,你有一個屬性ServerIP,我想你想被命名爲PropY,因爲你TextBox綁定到PropY財產。

<TextBox Name="propY" Text="{Binding PropY }" PreviewTextInput="textBox_PreviewTextInput"/

此外,你應該值在ExecuteResetFormCommand命令分配給您的財產,而不是你的私有成員由於私有成員不會觸發INotifyPropertyChanged

private void ExecuteResetFormCommand() 
{ 
    this.PropX = ""; 
    this.PropY = ""; // <-- Currently you have PropY declared as ServerIP 
} 
+0

忘記了ServerIP,我使用它,但我用propX和propy來簡化問題。我不知道私人成員不會觸發INotifyPropertyChanged,我認爲我的viewModel的所有成員都應該觸發它,因爲viewModel實現INotifyPropertyChanged。 – Stacked 2013-03-05 13:55:09

+1

不,即使您的ViewModel實現了'INotifyPropertyChange',也必須調用它才能觸發更新。這就是爲什麼'RaisePropertyChanged(()=> PropX);'是必需的,它會觸發'INotifyPropertyChanged'事件。另外,XAML不能讀取私有變量。 – 2013-03-05 14:02:14