2014-03-02 85 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

你能舉個例子嗎? –