How to change button background color depending on bound command canexecute ??

Posted by LaurentH on Stack Overflow See other posts from Stack Overflow or by LaurentH
Published on 2010-03-31T09:00:29Z Indexed on 2010/03/31 9:03 UTC
Read the original article Hit count: 274

Filed under:
|
|
|

Hi,

I Have a ItemTemplate in which is a simple button bound on a command, which can be executable or not depending on some property.

I'd like the color of this button's background to change if the command isn't executable. I tried several methods, but I can't find anyway to do this purely in XAML (I'm doing this in a study context, and code behind isn't allowed).

Here's my code for the button :

<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>

© Stack Overflow or respective owner

Related posts about xaml

Related posts about command