2013-03-05 51 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