Reputation: 1192
If I set ContentTemplate
on Content Presenter directly it works fine, if I use Style
it gets overridden:
Inside Buttons ControlTemplate
:
<ContentPresenter ContentTemplate="{StaticResource Default}"/>
vs
<ContentPresenter Style="{StaticResource MyContentStyle}"/>
Works fine:
<DataTemplate x:Key="Default">
<StackPanel>
<TextBlock>this is the</TextBlock>
<TextBlock>default view</TextBlock>
<TextBlock Text="{Binding}"></TextBlock>
</StackPanel>
</DataTemplate>
<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
<Grid Margin="20">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"
ContentTemplate="{StaticResource Default}"
/>
</Border>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource ButtonTemplate}">Click Me</Button>
Default DataTemplate
is not applied, it says overridden in visual tree.
this is the
default view
<Style x:Key="MyContentStyle" TargetType="ContentPresenter">
<Setter Property="ContentTemplate" Value="{StaticResource Default}"/>
</Style>
<ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
<Grid Margin="20">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"
Style="{StaticResource MyContentStyle}"
/>
</Border>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource ButtonTemplate}">Click Me</Button>
If I use ContentPresenter
independently from button it works fine:
<Grid>
<ContentPresenter Style="{StaticResource MyContentStyle}" Content="Work fine even with Style" />
</Grid>
Upvotes: 0
Views: 655
Reputation: 1192
If content is set on the ContentPresenter, the Style will be applied. So the solution for style to work is:
<ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource MyContentStyle}" />
Upvotes: 0
Reputation: 169370
The Button
has its own ContentTemplate
property. Set this one directly:
<Button Template="{StaticResource ButtonTemplate}" ContentTemplate="{StaticResource Default}">Click Me</Button>
Or in a Style
that you apply to the Button
:
<Style x:Key="ButtonStyle" TargetType="Button" >
<Setter Property="ContentTemplate" Value="{StaticResource Default}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Margin="20">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...
<Button Style="{StaticResource ButtonStyle}">Click Me</Button>
Upvotes: 1