Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top