【发布时间】:2019-02-21 20:36:10
【问题描述】:
我设置了一个 bool 属性并将其绑定到 xaml 中的 IsEnabled,但是 ICommand CanExecute 方法覆盖了 xaml 中的 IsEnabled,因此我的 bool 属性无效。
当我在视图模型的 CanExecute 方法中定义条件时,它要么禁用绑定该方法的所有按钮,要么启用所有按钮。
它是一个网格,每行显示 3 个不同的按钮,每个按钮都转到一个新的 xaml 屏幕。如果按钮所在行上的特定条件没有数据,则需要禁用该按钮。
如何进行设置,以便在某种情况下禁用按钮?
自定义命令:
public class CustomCommand : ICommand
{
private Action<object> execute;
private Predicate<object> canExecute;
public CustomCommand(Action<object> execute, Predicate<object> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add
{
}
remove
{
}
}
public bool CanExecute(object parameter)
{
//throw new NotImplementedException();
bool b = canExecute == null ? true : canExecute(parameter);
return b;
}
public void Execute(object parameter)
{
execute(parameter);
}
}
xml
<DataTemplate>
<Button Command="{Binding Source={StaticResource VM},
Path=Command}" CommandParameter="{Binding}" >
<SymbolIcon Symbol="Edit" Foreground="AliceBlue" />
</Button>
</DataTemplate>
可以在虚拟机中执行
private bool CanGetDetails(object obj)
{
return true;
}
【问题讨论】: