【问题标题】:WPF MVVM button command that gets textbox value获取文本框值的 WPF MVVM 按钮命令
【发布时间】:2015-05-12 16:00:22
【问题描述】:

我很难理解按钮命令的工作原理。我有这样的东西

{Binding TxtBox} 从模型中获取价值,假设它是“aaa”。我想点击按钮,值应该出现在第二个文本框中(带有{Binding TxtBox2}的那个)。

这是我的 xaml:

<TextBox Text="{Binding TxtBox, Source={StaticResource viewModel}}" />
<TextBox Text="{Binding TxtBox2, Source={StaticResource viewModel}}" />
<Button Command="{Binding ClickCommand}"/>

这是我的视图模型:

public class CommandHandler : ICommand
{
    private Action _action;
    private bool _canExecute;
    public CommandHandler(Action action, bool canExecute)
    {
        _action = action;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _action();
    }
}

我真的需要这个CommandHandler 课程吗?我从网上复制了代码。

public string TxtBox
{
    get { return Model.TxtBoxValue; }
    set { Model.TxtBoxValue = value; }
}
public string TxtBox2 { get; set; }

private ICommand _clickCommand;
public ICommand ClickCommand
{
    get
    {
        return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute)); // I believe that when the button is clicked MyAction() is triggered, right?

    }

}
private bool _canExecute = true;
public void MyAction()
{
    this.TxtBox2 = this.TxtBox; // should something like this work? Because right now it doesn't
}

【问题讨论】:

  • 'This is my ViewModel:' 紧随其后的是 ICommand 的实现。我认为这可能只是一个编辑问题,但您可能需要轻推标题以指示哪些部分实际上是您的 ViewModel。
  • 不直接相关,但您将希望在某处实现 INotifyPropertyChanged - 请参阅 msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx。这将通知 View 对 ViewModel 中的属性的任何更改。 “通常”的方法是创建一个实现 INotifyPropertyChanged 的​​基类,然后在您的 ViewModels 中从它继承。

标签: c# wpf xaml mvvm


【解决方案1】:

第二个文本框的绑定永远不会收到有关其绑定属性已更改的通知。当您设置 this.TxtBox2 时,您应该为该属性触发 propertychanged 事件,以便更新绑定。 See think link for everything on bindings

我不知道您是否将 prism 用作 mvvm 框架,但它与 DelegateCommand 类一起提供。我认为.net 框架中没有简单/轻量级的实现。 See this link for the mvvm framework and the delegate command

【讨论】:

    【解决方案2】:

    视图通过 PropertyChanged 事件对绑定更改做出反应,而您没有这些事件。让任何绑定到 View 的东西都实现 INotifyPropertyChanged,然后在 props 更改时触发事件,这样就可以让绑定工作(一种或两种方式)。

    将您的模型更改为如下所示,它应该适合您。

    
    public class MyViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members
    
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged = null;
    
        protected virtual void RaisePropertyChanged(string propName)
        {
            if(PropertyChanged != null)
            {
                Task.Run(() => PropertyChanged(this, new PropertyChangedEventArgs(propName)));
            }
        }
    
        #endregion
    
        public string TxtBox
        {
            get { return Model.TxtBoxValue; }
            set
            {
                Model.TxtBoxValue = value;
                RaisePropertyChanged("TxtBox");
            }
        }
    
        // presuming TxtBox2Value is in Model...else use a field
        public string TxtBox2
        {
            get { return Model.TxtBox2Value; }
            set
            {
                Model.TxtBox2Value = value;
                RaisePropertyChanged("TxtBox2");
            }
        }
    
        private ICommand _clickCommand;
        public ICommand ClickCommand
        {
            get
            {
                return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute)); // I believe that when the button is clicked MyAction() is triggered, right?
    
            }
    
        }
        private bool _canExecute = true;
        public void MyAction()
        {
            this.TxtBox2 = this.TxtBox; // should something like this work? Because right now it doesn't
        }
    }

    IMO - 最好让您的模型实现 INotifyPropertyChanged,然后直接绑定到它,而不是将其包装在您的 ViewModel 中。如果 Model : INotifyPropertyChanged,那么您的 ViewModel 现在看起来像这样:

    
    public class MyViewModel
    {
    
        // fire prop changed event here if this model will be swapped out after the ctor...otherwise don't worry about it
        public Model Model { get; set; }
    
        private ICommand _clickCommand;
        public ICommand ClickCommand
        {
            get
            {
                return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute)); 
    
            }
    
        }
        private bool _canExecute = true;
        public void MyAction()
        {
            Model = new Model();
    
            Model.TxtBox2 = "Some new value"; 
        }
    }

    ...您的 xaml 更改为:

    <TextBox Text="{Binding Model.TxtBox, Source={StaticResource viewModel}}" />
    <TextBox Text="{Binding Model.TxtBox2, Source={StaticResource viewModel}}" />
    <Button Command="{Binding ClickCommand}"/>
    

    【讨论】:

    • 我尝试了public class MyViewModel : INotifyPropertyChanged,但它并没有像我希望的那样工作。单击按钮时,我想将 TxtBox 的内容复制到 TxtBox2。
    • 我不知道头等舱是怎么做到的。该操作将值从 TxtBox 复制到 TxtBox2。您是否正在逐步执行以确保触发操作(即您的绑定是正确的)?
    猜你喜欢
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 2017-11-24
    • 2018-06-02
    • 1970-01-01
    • 2019-03-04
    • 2014-06-20
    相关资源
    最近更新 更多