1

我正在尝试在自定义控件(类似于按钮)上实现 ICommandSource。目前,该实现主要是显示在 ICommandSource 的 msdn 页面上以及显示在 ButtonBase 源代码中。

CanExecute 在加载控件时触发,但在任何属性更改时不会触发。传递给常规按钮的相同命令可以正常工作。当应该更改的属性发生更改时, CanExecute 触发并启用按钮。该命令是一个委托命令。

我试过 CommandManager.InvalidateRequerySuggested(); 但这并没有奏效。

有任何想法吗?

这是自定义控件中的实现:

private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    CollapsibleSplitButton csb = (CollapsibleSplitButton)d;
    csb.OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
}

private void OnCommandChanged(ICommand oldCommand, ICommand newCommand)
{
    if (oldCommand != null) UnhookCommand(oldCommand);
    if (newCommand != null) HookCommand(newCommand);
}

private void UnhookCommand(ICommand command)
{
    command.CanExecuteChanged -= OnCanExecuteChanged;
    UpdateCanExecute();
}

private void HookCommand(ICommand command)
{
    command.CanExecuteChanged += OnCanExecuteChanged;
    UpdateCanExecute();
}
private void OnCanExecuteChanged(object sender, EventArgs e)
{
    UpdateCanExecute();
}

private void UpdateCanExecute()
{
    if (Command != null)
        CanExecute = Command.CanExecute(CommandParameter);
    else
        CanExecute = true;
}

protected override bool IsEnabledCore
{
    get { return base.IsEnabledCore && CanExecute; }
}

我设置命令的地方:

...
    MyCommand = new DelegatingCommand(DoStuff, CanDoStuff);
...

private bool CanDoStuff()
{
    return (DueDate == null);
}

private void DoStuff() {//do stuff}
4

2 回答 2

1

使用delegate命令,CanExecuteChanged当您认为应该在 UI 上更新时,您必须明确提出。尝试使用此版本的命令RelayCommand-

public class RelayCommand<T> : ICommand
{
   #region Fields

   readonly Action<T> _execute = null;
   readonly Predicate<T> _canExecute = null;

   #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
    /// </summary>
    /// <param name="execute">Delegate to execute when Execute is called on the command.
    ///This can be null to just hook up a CanExecute delegate.</param>
    /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
    public RelayCommand(Action<T> 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<T> execute, Predicate<T> canExecute)
    {
       if (execute == null)
          throw new ArgumentNullException("execute");

       _execute = execute;
       _canExecute = canExecute;
    }

    #endregion

    #region ICommand Members

    ///<summary>
    ///Defines the method that determines whether the command can execute in its current 
    ///state.
    ///</summary>
    ///<param name="parameter">Data used by the command.  If the command does not require 
    /// data to be passed, this object can be set to null.</param>
    ///<returns>
    ///true if this command can be executed; otherwise, false.
    ///</returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute((T)parameter);
    }

    ///<summary>
    ///Occurs when changes occur that affect whether or not the command should execute.
    ///</summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    ///<summary>
    ///Defines the method to be called when the command is invoked.
    ///</summary>
    ///<param name="parameter">Data used by the command. If the command does not require 
    ///data to be passed, this object can be set to <see langword="null" />.</param>
    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }

    #endregion
}

并在您的班级中注册类似Delegate的命令-

public ICommand TestCommand { get; private set; }
TestCommand = new RelayCommand<object>(CommandMethod, CanExecuteCommand);

编辑

试着把CommandManager.InvalidateRequerySuggested();你的CanExecute-

private void OnCanExecuteChanged(object sender, EventArgs e)
{
    CommandManager.InvalidateRequerySuggested();
    UpdateCanExecute();
}
于 2012-10-24T06:31:24.200 回答
0

设法通过将回调包装在 EventHandler 中来解决问题。

private EventHandler currentHandler;

private void UnhookCommand(ICommand command)
{
    if (currentHandler != null)
        command.CanExecuteChanged -= currentHandler;
    UpdateCanExecute();
}

private void HookCommand(ICommand command)
{
    if (currentHandler == null) return;

    command.CanExecuteChanged += currentHandler;
    UpdateCanExecute();
}
于 2012-10-31T18:21:53.697 回答