【问题标题】:MVVM WPF Button Command Binding - RelayCommandMVVM WPF 按钮命令绑定 - RelayCommand
【发布时间】:2015-06-07 14:32:08
【问题描述】:

对不起,如果这是一个愚蠢的问题,但我似乎无法解决,基本上我在一个窗口上有一个用于保存数据的按钮。我添加了一个 RelayCommand 类,如此处所述(最佳答案) Binding Button click to a method 每次我运行我的项目时,都会点击 ICommand SaveCommand 方法。从这里它创建了一个循环并不断循环。但是,当窗口出现时,按下 Save 按钮实际上并不会导致 ICommand SaveCommand 方法被点击。我该如何解决这个问题?谢谢大家。

我的观点:

<av:Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="81" Height="44" Margin="52,0,0,20" av:Grid.RowSpan="2" Command ="{Binding SaveCommand}"/>

我的视图模型:

private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private ICommand _saveCommand;

    public ICommand SaveCommand
    {
        get
        {
            if (_saveCommand == null)
            {
                _saveCommand = new RelayCommand(
                    param => this.SaveObject(),
                    param => this.CanSave()
                    );
            }
            return _saveCommand;
        }
    }

    private bool CanSave()
    {
        // Verify command can be executed here

        return true;
    }

    private void SaveObject()
    {
        // Save command execution logic
    }

RelayCommand 类:

public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion // Fields

    #region Constructors

    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors

    #region ICommand Members

    [DebuggerStepThrough]
    public bool CanExecute(object parameters)
    {
        return _canExecute == null ? true : _canExecute(parameters);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameters)
    {
        _execute(parameters);
    }

    #endregion // ICommand Members
}

【问题讨论】:

  • 视图的数据上下文是否设置为等于视图模型?
  • 是的,DataContext 设置为此 ViewModel @user1
  • 您的窗口/Viewmodel 的构造函数中有任何代码吗?
  • 也应该是if(PropertyChanged != null)
  • 可重复执行 CanSave() 很好,因为需要对其进行评估以禁用/启用按钮。

标签: c# wpf mvvm


【解决方案1】:

你在SaveCommandget部分犯了一个错误/错字,你应该改变

if (_saveCommand != null)if (_saveCommand == null)

【讨论】:

  • 谢谢我已经改变了,但仍然看到相同的行为,即它在加载窗口时触发,而不是在单击按钮时触发,并且还会循环。
  • @DavidBeaumont,您可能需要向我们展示更多代码,因为您当前发布的代码无法产生错误。确保您没有在命令中错误地放置 CanSave()SaveObject()
【解决方案2】:

您的 CanSave 方法返回 false。

考虑解决这个问题。

【讨论】:

  • 我已经改变了这个,我仍然得到循环,当窗口加载时它也会触发。
  • 您能否更新您帖子中的代码以反映您的最新更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多