1

我的视图模型

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 = "";
    }

我的视图

<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);
}

重置命令重置我的视图模型中的属性,但文本框仍然包含它们的值,绑定无法正常工作:(我在这里遗漏了什么吗?

4

5 回答 5

3

您应该重置属性,而不是私有成员:

私人无效 ExecuteResetFormCommand() { this.PropX = ""; this.PropY = ""; }

于 2013-03-05T13:41:49.307 回答
0

您如何重置值?重置值时,您可能会覆盖数据绑定。如果您发布单击按钮时执行的代码,这将很有帮助。

于 2013-03-05T12:24:57.860 回答
0

在您的 xaml 代码中,您必须设置如下绑定:

<TextBox Name="propX" Text="{Binding PropX, Mode=TwoWay}" .../>
于 2013-03-05T12:26:26.150 回答
0

绑定必须是两种方式,以便文本框从视图模型更新自身

于 2013-03-05T12:33:55.830 回答
0

在您的代码隐藏中,您有一个 property 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
}
于 2013-03-05T13:50:25.547 回答