Mohnkuchenzentrale
Mohnkuchenzentrale

Reputation: 5885

Change ContentTemplate of Contentpresenter through Templatebinding

Is it possible to do something like this (See code below)? I tried different ways but none of them worked. Is it a limitation to TemplateBinding? I don't want to set the ContentTemplates via Staticressources (this works) because the Control should change Templates during runtime. How can I achieve this?

<ControlTemplate x:Key="myControlTemplate" TargetType="{x:Type controls:mycontrol}">
<Grid x:Name="root" Margin="{TemplateBinding Margin}" >
    <ContentPresenter Opacity="0" x:Name="Content" ContentTemplate="{TemplateBinding MinimizedContentTemplate}">
</Grid>
<ControlTemplate.Triggers>
    <Trigger Property="ControlState" Value="Maximized">
        <Setter TargetName="Content" Property="ContentTemplate" Value="{TemplateBinding MaximizedContentTemplate}"/>
    </Trigger>
</ControlTemplate.Triggers>

<Style TargetType="{x:Type controls:mycontrol}">
    <Setter Property="ControlState" Value="Minimized" />
    <Setter Property="MaximizedContentTemplate" Value="{StaticResource DefaultMaxContentTemplate}" />
    <Setter Property="MinimizedContentTemplate" Value="{StaticResource DefaultMinContentTemplate}" />
    <Setter Property="Template" Value="{StaticResource myControlTemplate}" />
</Style>

Upvotes: 1

Views: 2991

Answers (1)

Sheridan
Sheridan

Reputation: 69959

TemplateBinding does not seem to work correctly with custom properties on custom controls. You can try to use a RelativeSource Binding to the TemplatedParent property instead:

<ContentPresenter Opacity="0" x:Name="Content" ContentTemplate="{Binding 
    MinimizedContentTemplate, RelativeSource={RelativeSource TemplatedParent}}">

You can find out more from the TemplatedParent property page on MSDN.

Upvotes: 2

Related Questions