ControlTemplate that Respects Content


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="">
        
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Resources>
    <ControlTemplate x:Key="buttonTemplate" TargetType="{x:Type Button}">
      <Grid>
        <Ellipse x:Name="outerCircle" Width="100" Height="100">
          <Ellipse.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <GradientStop Offset="0" Color="Blue"/>
              <GradientStop Offset="1" Color="Red"/>
            </LinearGradientBrush>
          </Ellipse.Fill>
        </Ellipse>
        <Viewbox>
          <ContentControl Margin="20" Content="{TemplateBinding Content}"/>
        </Viewbox>
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter TargetName="outerCircle" Property="Fill" Value="Orange"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
          <Setter Property="RenderTransform">
            <Setter.Value>
              <ScaleTransform ScaleX=".9" ScaleY=".9"/>
            </Setter.Value>
          </Setter>
          <Setter Property="RenderTransformOrigin" Value=".5,.5"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Grid.Resources>
  <Button Template="{StaticResource buttonTemplate}">OK</Button>
</Grid>


</Window>

   
    
    
    
    
    
     


ControlTemplate with Triggers


   
      


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RSS Reader">
      
<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Resources>
    <ControlTemplate x:Key="buttonTemplate">
      <Grid>
        <Ellipse x:Name="outerCircle" Width="100" Height="100">
          <Ellipse.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <GradientStop Offset="0" Color="Blue"/>
              <GradientStop Offset="1" Color="Red"/>
            </LinearGradientBrush>
          </Ellipse.Fill>
        </Ellipse>
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="Button.IsMouseOver" Value="True">
          <Setter TargetName="outerCircle" Property="Fill" Value="Orange"/>
        </Trigger>
        <Trigger Property="Button.IsPressed" Value="True">
          <Setter Property="RenderTransform">
            <Setter.Value>
              <ScaleTransform ScaleX=".9" ScaleY=".9"/>
            </Setter.Value>
          </Setter>
          <Setter Property="RenderTransformOrigin" Value=".5,.5"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Grid.Resources>
  <Button Template="{StaticResource buttonTemplate}">OK</Button>
</Grid>

</Window>

   
    
    
    
    
    
     


Simple ControlTemplate


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RSS Reader">
    
<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid.Resources>
    <ControlTemplate x:Key="buttonTemplate">
      <Grid>
        <Ellipse Width="100" Height="100">
          <Ellipse.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <GradientStop Offset="0" Color="Blue"/>
              <GradientStop Offset="1" Color="Red"/>
            </LinearGradientBrush>
          </Ellipse.Fill>
        </Ellipse>
        <Ellipse Width="80" Height="80">
          <Ellipse.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
              <GradientStop Offset="0" Color="White"/>
              <GradientStop Offset="1" Color="Transparent"/>
            </LinearGradientBrush>
          </Ellipse.Fill>
        </Ellipse>
      </Grid>
    </ControlTemplate>
  </Grid.Resources>
  <Button Template="{StaticResource buttonTemplate}">OK</Button>
</Grid>



</Window>

   
    
    
    
    
    
     


ProgressBar with ControlTemplate


   
      

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="120" Width="300">
    <Window.Resources>
        <Style
            TargetType="{x:Type ProgressBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ProgressBar}">
                        <Grid MinHeight="20" MinWidth="240">
                            <Rectangle Name="PART_Track" Fill="Gainsboro" Stroke="Gray" StrokeThickness="1" />
                            <Rectangle Name="PART_Indicator" Fill="DarkGray" Stroke="Gray" StrokeThickness="1" HorizontalAlignment="Left" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <ProgressBar Width="200" Height="100"  x:Name="progress" Value="30" HorizontalAlignment="Center" Margin="10"/>
        <Slider Value="{Binding ElementName=progress, Path=Value, Mode=TwoWay}" Minimum="0" Maximum="100"  Margin="10"/>
    </StackPanel>

</Window>

   
    
    
    
    
    
     


Specify Named Parts of a Control Template


   
      

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="120" Width="300">
    <Window.Resources>
        <Style
            TargetType="{x:Type ProgressBar}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ProgressBar}">
                        <Grid MinHeight="20" MinWidth="240">
                            <Rectangle Name="PART_Track" Fill="Gainsboro" Stroke="Gray" StrokeThickness="1" />
                            <Rectangle Name="PART_Indicator" Fill="DarkGray" Stroke="Gray" StrokeThickness="1" HorizontalAlignment="Left" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <StackPanel>
        <ProgressBar  Width="200" Height="100" x:Name="progress" Value="30" HorizontalAlignment="Center" Margin="10"/>
        <Slider Value="{Binding ElementName=progress, Path=Value, Mode=TwoWay}" Minimum="0" Maximum="100"  Margin="10"/>
    </StackPanel>
</Window>

   
    
    
    
    
    
     


Label with ControlTemplate


   
      


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="100" Width="180">

    <Window.Resources>
        <ControlTemplate x:Key="labelTemplate" TargetType="{x:Type Label}">
            <Border x:Name="border" CornerRadius="4" BorderThickness="3" BorderBrush="DarkGray" Background="{TemplateBinding Property=Background}">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Label Width="100" Height="24" 
               Margin="4" Content="Custom Label"
               Template="{StaticResource labelTemplate}"
               Background="Red"/>
    </Grid>
</Window>

   
    
    
    
    
    
     


Put a Control Template into a Style


   
      
<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="WPF" Height="120" Width="260">

    <Window.Resources>
        <Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Width" Value="36"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate 
                        TargetType="{x:Type ToggleButton}">
                        <Canvas >
                            <Path x:Name="pth" Stroke="#000080" Fill="#C0C0C0" 
                              StrokeThickness="3" StrokeStartLineCap="Round" 
                              StrokeEndLineCap="Round" StrokeLineJoin="Round" 
                              Data="M 10,100 l 100,0 l 18,-10 l 5,110 l 10,0 l -7,110 l 2,210 l -20,-15 l -110,5 l 2,-110 Z" />
                        </Canvas>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>

    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10" Orientation="Horizontal">
        <ToggleButton IsChecked="True"/>
        <ToggleButton IsChecked="False"/>
    </StackPanel>

</Window>