views:

59

answers:

1

I am creating a custom control.

I want the template for this control to use different controls for the root control based on the value of a dependency property called CanExpand. CanExpand is defined in the custom control class.

If CanExpand is true, I want to display using an Expander:

<ControlTemplate ...>
   <Expander ...>
      <!--...-->
      <ContentPresenter/>
   </Expander>
</ControlTemplate>

If CanExpand is false, I want to display using a HeaderedContentControl instead:

<ControlTemplate ...>
   <HeaderedContentControl ...>
      <!--...-->
      <ContentPresenter/>
   </HeaderedContentControl>
</ControlTemplate>

I thought of using a DataTemplateSelector, but this is a ControlTemplate not a DataTemplate and there is no selector property for the Template of a control.

I can't set the different controls to visible/hidden with a Trigger because the child content can only live under one control. Also, I don't think you can change the Content property using a Trigger.

Any suggestions?

Thanks.

+2  A: 

Inside your Style, set the ControlTemplate property for the default state and then have a Trigger which sets the ControlTemplate property to a different template. For example:

<Style ...>
    <Setter Property="ControlTemplate">
        <ControlTemplate ...>    
        </ControlTemplate>
    </Setter>
    <Style.Triggers>
        <Trigger Property="YourProperty" Value="WhateverValue">
            <Setter Property="ControlTemplate">
                <ControlTemplate ...>
                </ControlTemplate>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Keep in mind you can have a trigger on the same property for multiple values, each value getting a completely different template.

Drew Marsh
I thought you could only have EventTriggers inside of styles?
Josh G
No, property triggers are allowed in styles. It's when you're putting triggers directly on the element that you can only use EventTriggers.
Drew Marsh
That's correct answer :)
Anvaka
OK, great thanks much!
Josh G