Reputation: 2673
As I've a resource style control template for Button, In addition I've defined a control template in button itself.But the content of the Button didnot displaying. How can I resolve this ?
<Grid>
<Grid.Resources>
<Style TargetType="Button">
<!--Set to true to not get any properties from the themes.-->
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Background" Value="Yellow"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Border TextBlock.Foreground="{TemplateBinding Foreground}"
x:Name="Border"
CornerRadius="10"
BorderThickness="1"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button Name="test" Width="50" Height="60" Content="myvalue">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
Upvotes: 2
Views: 1110
Reputation: 19296
You should set TargetType
in ControlTemplate
to Button
.
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
Explanation (source msdn):
If you have a standalone ControlTemplate in the resources section with the TargetType property set to a type, the ControlTemplate does not get applied to that type automatically. Instead, you need to specify an x:Key and apply the template explicitly.
Also note that the TargetType property is required on a ControlTemplate if the template definition contains a ContentPresenter.
Upvotes: 1