tags:

views:

82

answers:

3

I'm trying to create a set of button which have an off or on state, much a checkbox without the check. Ideally I want the colour to change to represent the two different states off(red), green(on). I've tried setting a control template but this only changes the colour for a selection, then reverts back to it's original colour once the mouse leaves the button's vicinity.

<ControlTemplate.Triggers>
   <Trigger Property="IsPressed" Value="True">
      <Setter Property="Background" TargetName="Background" Value="Green"/>
   </Trigger>
</ControlTemplate.Triggers>
+2  A: 

First, what you describe is a ToggleButton.
Second, use a 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

Muad'Dib
Actually, I think what he wants is not the mouse hover, but the state ispressed. Otherwise, I'm with you on that one :)
David Brunelle
ooops, your right.
Muad'Dib
Togglebutton is much better, although the colour still aren't working.
wonea
A: 

Use something similar to 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>
Taylor Leese
A: 

Actually, could use a CheckBox for that.Allow me to explain :

WPF let you define the control template, which is basically the whole control itself. You could create a checkbox that looks exactly like a button.

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

David Brunelle