绑定静态命令变量变灰按钮?
Binding Static ICommand Variable grays out button?
继续我与静态变量和 XAML 的斗争,我无法解决命令绑定使按钮变灰的问题。
视图模型中的代码:
public static ICommand CancelCalender => _cancelCalender
?? (_cancelCalender = new CommandHandler(CancelCalender_Button, _canExecute));
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();
}
}
引用,
xmlns:viewModels="clr-namespace:StaffShiftManager.ViewModels"
这些是我尝试绑定命令变量的方法:
Command="{x:Static viewModels:ViewModelBase.CancelCalender}"
和
Command="viewModels:ViewModelBase.CancelCalender"
和
Command="{Binding Source={x:Static viewModels:ViewModelBase.CancelCalender}}"
有什么我想念的吗?任何帮助将不胜感激。
基本上在 ICommand CanExecute()
方法中,这个 returns 无论命令是否启用\都可以执行。
从 CanExecute()
返回 false
将禁用(灰色)按钮。
现在我稍微修改了您的代码以提供 Func<bool>
作为 CanExecute()
处理程序。这里会发生的是每次重新查询命令执行时它都会执行您的 canExecute
方法。
public class CommandHandler : ICommand
{
public CommandHandler(Action execute)
:this(execute, null)
{
}
public CommandHandler(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_executeHandler = execute;
_canExecuteHandler = canExecute ?? (() => true);
}
Func<bool> _canExecuteHandler = () => true;
Action _executeHandler;
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return _canExecuteHandler();
}
public void Execute(object parameter)
{
_executeHandler?.Invoke();
}
}
并不是说 canExecute
方法的默认实现是 return true
。即使将 null Func
传递给构造函数仍将导致 true.
再补充一点,我最喜欢的命令绑定器之一(比上面的更高级)正在使用 DelegateCommand
。我不记得我在哪里找到原始来源(因为我没有写它)但是更高级。
继续我与静态变量和 XAML 的斗争,我无法解决命令绑定使按钮变灰的问题。
视图模型中的代码:
public static ICommand CancelCalender => _cancelCalender
?? (_cancelCalender = new CommandHandler(CancelCalender_Button, _canExecute));
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();
}
}
引用,
xmlns:viewModels="clr-namespace:StaffShiftManager.ViewModels"
这些是我尝试绑定命令变量的方法:
Command="{x:Static viewModels:ViewModelBase.CancelCalender}"
和
Command="viewModels:ViewModelBase.CancelCalender"
和
Command="{Binding Source={x:Static viewModels:ViewModelBase.CancelCalender}}"
有什么我想念的吗?任何帮助将不胜感激。
基本上在 ICommand CanExecute()
方法中,这个 returns 无论命令是否启用\都可以执行。
从 CanExecute()
返回 false
将禁用(灰色)按钮。
现在我稍微修改了您的代码以提供 Func<bool>
作为 CanExecute()
处理程序。这里会发生的是每次重新查询命令执行时它都会执行您的 canExecute
方法。
public class CommandHandler : ICommand
{
public CommandHandler(Action execute)
:this(execute, null)
{
}
public CommandHandler(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_executeHandler = execute;
_canExecuteHandler = canExecute ?? (() => true);
}
Func<bool> _canExecuteHandler = () => true;
Action _executeHandler;
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return _canExecuteHandler();
}
public void Execute(object parameter)
{
_executeHandler?.Invoke();
}
}
并不是说 canExecute
方法的默认实现是 return true
。即使将 null Func
传递给构造函数仍将导致 true.
再补充一点,我最喜欢的命令绑定器之一(比上面的更高级)正在使用 DelegateCommand
。我不记得我在哪里找到原始来源(因为我没有写它)但是更高级。