首先;確保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 ... –