ferday
ferday

Reputation: 197

WPF changing button color, with a defined style already in place

i've made some rounded buttons, using a template. hovering the button changes the colour

MainWindow

<Button Grid.Column="1" x:Name="pressbtn" Command="{Binding TypeChange}" 
  CommandParameter="{Binding Name ,RelativeSource={RelativeSource Self}}" 
  Style="{StaticResource btnBlue}" BorderBrush="MidnightBlue" 
  HorizontalAlignment="Stretch">Press</Button>

Style Resource

<Style TargetType="Button" x:Key="btnBlue">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="#000"/>
    <Setter Property="FontSize" Value="13"/>
    <Setter Property="Margin" Value="5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="0,1,1,0"
                        CornerRadius="10">
                    <ContentPresenter HorizontalAlignment="Center" 
                                      VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="MidnightBlue"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

this works fine, but i'd like the button to stay on a background colour until another button is pressed, upon which it would go transparent and the other button would change colour

i have tried (where IsSelected is an integer, with each button triggering the property)

<DataTrigger Binding="{Binding IsSelected}" Value="0">
        <Setter Property="Background" Value="MidnightBlue"/>
</DataTrigger>

i've also tried

<Trigger Property="IsPressed" Value="True">
     <Setter Property="Background" Value="MidnightBlue"/>
     <Setter Property="Foreground" Value="White"/>
</Trigger>

it seems that any of the button events (like IsPressed) are transient, so a button press works for a brief period but wont stick. reading up about the , it appears that it doesn't work because i set the Background inline in the main Setter

<Setter Property="Background" Value="Transparent"/>

However, if i take out that Property, nothing changes

this part of XAML is a weak point (Styles, Templates), and i can't find anything in searching that helps or at least that i understand very well. Any assistance in getting the <DataTrigger> or other method to work is appreciated

Upvotes: 0

Views: 116

Answers (1)

mm8
mm8

Reputation: 169150

If you want a "sticky" version of IsPressed, you should use a ToggleButton and bind to its IsChecked property.

An ordinary Button has no property to indicate whether it has previously been clicked or not.

You will of course also have to write some code that sets the IsChecked property back to false or true when the other Button is clicked.

If IsChecked doesn't fits your requirements, there is the Tag property that you can set to anything.

Upvotes: 1

Related Questions