Search code examples
c#wpfbuttonstylesfluent

How to implement a two-state button


I have a simple button, and I would like it to have the behavior of a "sticky" button, such as it visually has two states (much like a switch). In other words, I would like it to maintain its pressed style even after I release the mouse button, but it would have to be clickable.

Is there a way to easily do it in WPF? I'm using Fluent but I could change it if needed.

<Fluent:Button Header="myButton" Command="{Binding ToggleCommand}" SizeDefinition="Small">
    <Fluent:Button.Icon>
       <Grid Width="16" Height="16">
           <Image Source="../Images/16x16/icon.png"/>
       </Grid>
    </Fluent:Button.Icon>
</Fluent:Button>

Something like this:

1- not-pressed 2- pressed

PS:

I'm using MVVM, and in the Command, I'm checking and changing a bool flag to know what state the button currently is. I would be able to bind it to a button property, if it helps in any way.


Solution

  • Turns out there is a component ToggleButton that does exactly what I was looking for. Silly me for not knowing it existed in the first place.