2014-03-02 128 views
0

我有一个按钮,它绑定到命令将状态从启用更改为禁用。 当按钮被禁用我不能点击它(没有响应...)但它的 颜色仍然像以前一样(如启用模式),我怎么能 当按钮被禁用给它的颜色像变灰。按钮禁用但不会变灰

<Button Command="{Binding Select}" 
         CommandParameter="{Binding ElementName=SelWindow}" 
         Width="125" 
         Height="26" 
         Background="#f0ab00" 
         Content="Run" 
         IsEnabled="{Binding SelectServEnabled}" 

         FontSize="16" 
         Foreground="#ffffff" 
         Margin="0,0,80,10" /> 
+1

你忽略某个按钮的风格? –

+0

@ FlatEricnop ... –

回答

0

首先;确保SelectServEnabled已正确定义。

其次;命令包含一个CanExecute方法,用于更改按钮的启用/禁用状态。不要使用IsEnabled,而是在命令的CanExecute方法中处理它。

第三件;作为FlatEric说有样式替代的机会很高(检查你的主题文件夹或Generic.xaml或类似的东西,并检查任何风格的ButtonTargetType

我测试过你这样的代码,它工作正常:

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) 
    { 
    } 
} 
+0

你能举个例子吗? –