2010-03-31 34 views
4

我有一個ItemTemplate,其中是一個簡單的按鈕綁定在一個命令,可以執行或不取決於某些屬性。如何根據綁定命令更改按鈕背景顏色canexecute?

我想要這個按鈕的背景顏色改變,如果命令不可執行。 我嘗試了幾種方法,但我無法找到純粹在XAML中完成的操作(我在學習上下文中執行此操作,並且不允許使用代碼)。

下面是我對按鈕的代碼:

<Button x:Name="Dispo" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="30" Height="30" 
     Grid.Column="2" Grid.Row="0" 
     Command="{Binding AddEmpruntCommandModel.Command}" 
     CommandParameter="{Binding ElementName='flowCars', Path='SelectedItem'}" 
     vm:CreateCommandBinding.Command="{Binding AddEmpruntCommandModel}" > 
    <Button.Style> 
     <Style TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="True"> 
        <Setter Property="Button.Background" Value="Green"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="Button.Background" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
    </Button> 

回答

0

您可以指定自己的模板那樣:

<Button Content="OK" Command="{Binding SomeCommand}"> 
    <Button.Style> 
     <Style> 
      <Setter Property="Button.Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border x:Name="Border" Background="Green"> 
          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
         </Border> 
         <ControlTemplate.Triggers>           
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter TargetName="Border" Property="Background" Value="Red" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Button.Style> 
</Button> 
相關問題