Control Templates
|
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="buttonTemplate">
<Grid Width="100" Height="30">
<Rectangle Fill="Bisque" Stroke="Brown" RadiusX="5" RadiusY="5"/>
<Ellipse Width="24" Height="24" Fill="Brown" HorizontalAlignment="Left" Margin="3"/>
</Grid>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource buttonTemplate}"/>
</Grid>
|
Control Template Trigger
|
<Grid>
<Grid.Resources>
<ControlTemplate x:Key="buttonTemplate">
<Grid Width="100" Height="30">
<Rectangle Fill="Bisque" Stroke="Brown" RadiusX="5" RadiusY="5"/>
<Ellipse x:Name="Icon" Width="24" Height="24" Fill="Brown" HorizontalAlignment="Left" Margin="3"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter TargetName="Icon" Property="Fill" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Grid.Resources>
<Button Template="{StaticResource buttonTemplate}">OK</Button>
</Grid>
|
Restricting the Target Type
|
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
<Grid Width="100" Height="30">
…
</Grid>
</ControlTemplate>
|
Content Property
|
<TextBlock Text="{TemplateBinding Content}" Margin="36,0,0,0" VerticalAlignment="Center"/>
|
|
<ContentPresenter Content="{TemplateBinding Content}"/>
|
Other Properties
|
<ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
<Grid Width="100" Height="30">
<Rectangle Fill="{TemplateBinding Background}" Stroke="Brown" RadiusX="5" RadiusY="5"/>
<Ellipse x:Name="Icon" Width="24" Height="24" HorizontalAlignment="Left" Margin="3">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0, 1">
<GradientStop Offset="0"
Color="{Binding RelativeSource={RelativeSource
TemplatedParent}, Path=Foreground.Color}"/>
<GradientStop Offset="1" Color="Red"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter Content="{TemplateBinding Content}" Margin="36,0,0,0" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
|
Control Parts
Control States
Mixing Templates With Styles
|
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
…
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Button>OK</Button>
</Grid>
|