#10 – Control Templates
July 22, 2010 1 Comment
Similar to styles in WPF, but different, are templates. Templates allow you to replace all aspects of a UI control’s appearance without changing its behavior.
Every control in WPF has a default template that completely specifies its appearance. You can replace the default template with a template that you’ve authored, to change the appearance of the control.
You change a control’s template by changing its Template property. Here’s an example where we create a new button template that gives the button a thick blue border.
<Window.Resources> <ControlTemplate x:Key="Crazy" TargetType="Button"> <Border BorderBrush="Blue" Background="White" BorderThickness="3"> <ContentPresenter></ContentPresenter> </Border> </ControlTemplate> </Window.Resources> <Grid Height="auto" Width="503"> <StackPanel Height="100" HorizontalAlignment="Left" Margin="85,139,0,0" Name="stackPanel1" > <Button Content="Crazy template" Template="{StaticResource Crazy}" /> <Button Content="Default template" /> </StackPanel> </Grid>