我正在尝试在自定义控件(类似于按钮)上实现ICommandSource。当前,该实施方式大多就像它显示在MSDN页面上的ICommandSource以及在ButtonBase源代码中显示的。
。可以在控件的负载上发射射击,但在任何属性更改时不会发射。传递给常规按钮的同一命令正常工作。当应该更改更改的属性时,可以启用火灾和按钮。该命令是委托命令。
我尝试了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}
使用delegate
命令,您必须在认为应该在UI上更新它时明确提高CanExecuteChanged
。尝试使用此版本的命令称为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();
}
通过将回调包裹在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();
}