Jonny Piazzi
Jonny Piazzi

Reputation: 3784

User Control Inherited From Button in WPF

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?

Upvotes: 1

Views: 871

Answers (2)

Sinatr
Sinatr

Reputation: 21969

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.

Upvotes: 3

Edmund Covington
Edmund Covington

Reputation: 521

You can use the WPF toggle button.

Upvotes: 0

Related Questions