开发者

WPF Button on or off coloured state

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开发者_C百科 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>


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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜