views:

128

answers:

1

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

You can specify your own template like that:

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