【问题标题】:CanExecute of ApplicationCommand does not updateApplicationCommand 的 CanExecute 不更新
【发布时间】:2015-07-15 02:08:39
【问题描述】:

我想用 MenuItem-Click 调用 ApplicationCommand:

        <MenuItem Header="{StaticResource MenuItemSave}" Command="ApplicationCommands.Save"/>

在我的 ViewModel-Constructor 中,我初始化我的绑定:

    CommandBinding saveBinding = new CommandBinding(ApplicationCommands.Save, SaveCommand_Execute, SaveCommand_CanExecute);
    CommandManager.RegisterClassCommandBinding(typeof(ViewModel_Main), saveBinding);
    RegisterCommandBindings.Add(saveBinding);

现在我想处理该命令,但它根本无法执行。即使它应该总是正确的。

private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}
private void SaveCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
    //Stuff
}

我还尝试在我的 init 函数之后更新所有绑定:

CommandManager.InvalidateRequerySuggested();

但我的 MenuItem 仍然处于禁用状态。

谢谢!

【问题讨论】:

    标签: c# wpf mvvm command canexecute


    【解决方案1】:

    我建议使用这个 ICommand 实现

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Input;
    
    namespace Common
    {
        public class Command<TArgs> : ICommand
        {
            public Command(Action<TArgs> exDelegate)
            {
                _exDelegate = exDelegate;
            }
    
            public Command(Action<TArgs> exDelegate, Func<TArgs, bool> canDelegate)
            {
                _exDelegate = exDelegate;
                _canDelegate = canDelegate;
            }
    
            protected Action<TArgs> _exDelegate;
            protected Func<TArgs, bool> _canDelegate;
    
            #region ICommand Members
    
            public bool CanExecute(TArgs parameter)
            {
                if (_canDelegate == null)
                    return true;
    
                return _canDelegate(parameter);
            }
    
            public void Execute(TArgs parameter)
            {
                if (_exDelegate != null)
                {
                    _exDelegate(parameter);
                }
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { CommandManager.RequerySuggested += value; }
                remove { CommandManager.RequerySuggested -= value; }
            }
    
            bool ICommand.CanExecute(object parameter)
            {
                if (parameter != null)
                {
                    var parameterType = parameter.GetType();
                    if (parameterType.FullName.Equals("MS.Internal.NamedObject"))
                        return false;
                }
    
                return CanExecute((TArgs)parameter);
            }
    
            void ICommand.Execute(object parameter)
            {
                Execute((TArgs)parameter);
            }
    
            #endregion
        }
    }
    

    【讨论】:

    • 我认为这与 MVVM 轻框架中的实现相同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多