views:

952

answers:

1

I am creating a simple custom control extending from toggle button that allows the user to specify checked and unchecked content directly in XAML. It works well but it is based on a trigger, and I don't know how to define the trigger except in a style. If I define the style, then I lose anything set outside of the custom control.

What I would like to be able to do is just append this trigger to any existing style set elsewhere on the control.

Here's the XAML for the style/trigger.

<ToggleButton.Style>
    <Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
        <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Content"
                        Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ToggleButton.Style>

I tried inheriting the style via the BasedOn with a default type but it won't work if the custom control has an explicit style set by its parent. I also considered EventTriggers but I do not believe there would be an event to initialize the control.

Thanks for any help anyone can offer. :)

+3  A: 

Just to clear things up on the terminology here: A user control is a control that derives from the UserControl class. If I understood you right you derived from ToggleButton to add the UncheckedContent and CheckedContent properties. In that case you have created a custom control. Its always easier to follow if we agree on common terminology :)

As far as I know you can not do such a generic style inheritance in xaml. You always have to specifiy explictly what style a anothet style is based upon. Your style can either be based on the default style for ToggleButton or on a specific other style. If you can't build a style inheritance chain that respects that, this approach won't work.

But since you have a custom control, couldn't you write a default style for it that is based on the default toggle button style like this?

<Style TargetType="{x:Type CustomToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">

Then whenever you apply an explicit style to a toggle button you would specify that it is based on the default toggle button style.

Also you could write a (default) control template for your new toggle button in Themes\Generic.xaml that contains the above triggers. In blend you can get a copy of the default template for toggle button ("Edit Template"->"Edit a Copy") so you can make sure that your toggle button looks exactly like the normal toggle button. Then incorporate the triggers above into that template.

BTW: you do not have to create a new control just to add new properties. You can add new properties to an exisiting control using attached properties. They can be used from XAML just like normal properties.

bitbonk
Thanks for the input, it was very helpful. I went a little different route and extended the existing style in code behind to add my trigger without ruining the existing one. The BasedOn suggestion you made I had already tried and does not work if an explicit style is set. I got there from your ideas though so I'm marking it as the answer.The attached properties idea was really good and I made that change as well, thank you. :)
Steve Cadwallader
Would you mind posting some details of what you did? I'd like to do the same thing.
skypecakes