WPF button in color or off

I am trying to create a set of buttons with the state turned off or on, in most cases a check box without checking. Ideally, I want the color to change to display two different states (red), green (on). I tried to install a control template, but it only changes the color for the selection, and then returns to the original color when the mouse leaves the button nearby.

<ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" TargetName="Background" Value="Green"/> </Trigger> </ControlTemplate.Triggers> 
+4
source share
3 answers

First, you describe ToggleButton.
Secondly, use the style and triggers for "IsChecked"

 <Style x:Key="MyToggleStyle" TargetType="{x:Type ToggleButton}"> <Style.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter Property="Background" Value="Green"/> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter Property="Background" Value="Red"/> </Trigger> </Style.Triggers> </Style> 

here is a solution to a similar problem

+6
source

Use something similar below:

 <Button> ... <Button.Style> <Style> <Style.Triggers> <DataTrigger Binding="{Binding SomeField, Converter={StaticResource yourConverter}}" Value="yourValue"> <!-- set what you want here --> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 
0
source

In fact, it can use CheckBox for this. Let me explain:

WPF allows you to define a control pattern that basically represents the entire control. You can create a checkbox that looks exactly like a button.

However, as someone said, ToggleButton is probably what you want to use.

0
source

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


All Articles