How to change the background color of a button depending on the associated canececute command?

I have an ItemTemplate in which there is a simple button associated with a command that can be executed or is not dependent on any property.

I want the background color of this button to change if the command is not executable. I tried several methods, but I still can’t do it exclusively in XAML (I do this in the context of the study, and the code behind is not allowed).

Here is 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> 
+4
source share
1 answer

You can specify your own template as follows:

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

Source: https://habr.com/ru/post/1305644/


All Articles