首先;确保SelectServEnabled已正确定义。
其次;命令包含一个CanExecute
方法,用于更改按钮的启用/禁用状态。不要使用IsEnabled
,而是在命令的CanExecute方法中处理它。
第三件;作为FlatEric说有样式替代的机会很高(检查你的主题文件夹或Generic.xaml
或类似的东西,并检查任何风格的Button
TargetType
)
我测试过你这样的代码,它工作正常:
public ICommand Select
{
get { return (ICommand)GetValue(SelectProperty); }
set { SetValue(SelectProperty, value); }
}
public static readonly DependencyProperty SelectProperty =
DependencyProperty.Register("Select", typeof(ICommand), typeof(MainWindow), new UIPropertyMetadata(null));
public bool SelectServEnabled
{
get { return (bool)GetValue(SelectServEnabledProperty); }
set { SetValue(SelectServEnabledProperty, value); }
}
public static readonly DependencyProperty SelectServEnabledProperty =
DependencyProperty.Register("SelectServEnabled", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));
这是一个简单的命令:
public class MyCommand : ICommand
{
public bool CanExecute(object parameter)
{
//here you can change the enabled/disabled state of
//any button that bind to this command
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
}
}
你忽略某个按钮的风格? –
@ FlatEricnop ... –