1

I was asked to create a Radio and CheckBox variation were visually similar to a button.

With this behavior:
When the button is clicked, it change to state checked, the background is changed, when clicked again the state is changed to unchecked and the background turn into the original brush.

At first my strategy was to create a user control. But since my control will be almost equal to a button, make sense to me use inheritance.

So my question is
Is possible to create a user control that inherit from button? If so, is that a good approach? How can I do it?

Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81
  • 1
    [`ToggleButton`](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton(v=vs.110).aspx) with 2 templates based on `IsChecked`. See e.g. [this](http://stackoverflow.com/a/15315930/1997232). – Sinatr Apr 05 '16 at 14:20
  • @Sinatr can you put this in a answer with a snippet code perhaps? – Jonny Piazzi Apr 05 '16 at 14:29
  • `ToggleButton` really fits for your requirements. Just write in your XAML – StepUp Apr 05 '16 at 15:03

2 Answers2

3

One possible approach is to use ToggleButton, but completely change its appearance when IsChecked become true:

<ToggleButton>
    <ToggleButton.Style>
        <Style TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>

                        <!-- IsChecked == false template -->

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Trigger.Setters>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>

                                    <!-- IsChecked == true template -->

                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Trigger.Setters>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ToggleButton.Style>
</ToggleButton>

Use 2 different templates (e.g. <TextBlock Text="On" /> and <TextBlock Text="Off" />) to see how it works.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

You can use the WPF toggle button.

Edmund Covington
  • 521
  • 6
  • 17
  • How so?, I look here in "Toolbox/All WPF Controls" and can't find this ToggleButton. I'm using Visual Studio 2015 Community. Do I need to do something to make this item appear? – Jonny Piazzi Apr 05 '16 at 14:25