Wpf绑定无法正常工作

时间:2013-03-05 12:18:31

标签: wpf mvvm binding

我的ViewModel

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命令重置视图模型中的属性,但文本框仍包含其值,绑定无法正常工作:( 我在这里错过了什么吗?

5 个答案:

答案 0 :(得分:3)

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

private void ExecuteResetFormCommand()     {         this.PropX =“”;         this.PropY =“”;     }

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

为了让textbox从viewmodel

更新自己,

绑定必须是两种方式

答案 4 :(得分: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
}