Change button background color due to events and view model property

Yaron

I have a button that can be at one of three modes:

  • Normal - No mouse hover and not selected
  • Hover - Mouse hover and not selected
  • Selected - A property at the view model (DataContext) called IsSelected equals true

This is my current code:

<Style x:Key="FlatButtonStyle" TargetType="Button">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Orange"/>
            </Trigger>
            <DataTrigger Binding="{Binding IsSelected}">
                <Setter Property="Background" Value="Yellow"/>
            </DataTrigger>
        </Style.Triggers>
        <Setter Property="Background" Value="Gray" />           
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Width" Value="125"/>
        <Setter Property="Height" Value="35"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border BorderThickness="0"
                            Background="{TemplateBinding Background}"
                            CornerRadius="12">
               <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>    

However, only the Hover and normal states work. I tried data triggers, visual state manager but I'm new to WPF and couldn't make it work.

Juergen

Your IsSelected Trigger Value is missing. If you want to stick with a button use

<DataTrigger Binding="{Binding IsSelected}" Value="True">
    <Setter Property="Background" Value="Yellow"/>
</DataTrigger>

I will recommend to use ToggleButton and Trigger on IsChecked Property

<ToggleButton IsChecked="{Binding IsSelected}" Style="{StaticResource FlatButtonStyle}"/>

<Style x:Key="FlatButtonStyle" TargetType="ToggleButton">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Background" Value="Yellow"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="Background" Value="Gray" />
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Width" Value="125"/>
    <Setter Property="Height" Value="35"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Border BorderThickness="0"
                Background="{TemplateBinding Background}"
                CornerRadius="12">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change button text color and background color while button is pressed

Button to change background color randomly

In universal windows apps, how to change the background color of a button using xaml and databinding if a property in the view model changes

Change background color of SearchView button and query

Change background color of the button on click

Change button background color and save older background

Change button color and background color onclick

How to change background color of button while clicking on it

Change TreeViewItem Color by property of model

How to change text background color of a button in Android?

How to change the background color of a button?

change the background color of the button

Change background button color on click

Change the background color of a button

Change background color of active button

how to change the background color size in a button?

Image in bootstrap button change with the background color and not the button color

Python tkinter change background color with a button

How to change background color with toggle button in React?

Change other view's background color by button state

Bootstrap and JavaScript button change icon with background color

How to Change Button Background color on click of the button

Unable to change button background color on click

JToggle Button background color change

Background color not change TextEditor view

How to change button color based on background?

Getting background color from view model property

Change background color of dropdown button- flutter

How to change a button's background color