title: Defining a control template for a rectangle in XAML
body: <p>In my current project, I have this XAML file where I define the visual style that must be applied to different types of custom widgets.</p>
  
  <p>For example, the style for a 'DirectLineButton' (a custom class that inherits from WPF's Button) is as follows:</p>
  
  <pre><code>    &lt;Style x:Key="DirectLineButtonTemplate" TargetType="{x:Type View:DirectLineButton}"&gt;
  &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
  &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
  &lt;Setter Property="MinHeight" Value="23"/&gt;
  &lt;Setter Property="MinWidth" Value="75"/&gt;
  &lt;Setter Property="Template"&gt;
      &lt;Setter.Value&gt;
      &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
          &lt;Border x:Name="MyBorder"  
                 CornerRadius="2" 
                 BorderThickness="2"
                 Background="Gold"
                 BorderBrush="Gray"&gt;
          &lt;ContentPresenter Margin="2"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               RecognizesAccessKey="True"/&gt;
          &lt;/Border&gt;
          &lt;ControlTemplate.Triggers&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineAvailable"&gt;
              &lt;Setter TargetName="MyBorder" Property="Background" Value="Gold"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineIdle"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="Silver"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineBusy"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="Green"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineCalled"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="LightBlue"/&gt;
          &lt;/Trigger&gt;
          &lt;/ControlTemplate.Triggers&gt;
      &lt;/ControlTemplate&gt;
      &lt;/Setter.Value&gt;
  &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Now, I need to replicate this idea by defining the style for a Rectangle. Unfortunately, WPF flags an error message when I try to define a ControlTemplate for a rectangle, can you suggest a workaround for this? See the code below of what I'm trying to attempt:</p>
  
  <pre><code>    &lt;Style x:Key="MyRectangleTemplate" TargetType="{x:Type Rectangle}"&gt;
  &lt;Setter Property="SnapsToDevicePixels" Value="true"/&gt;
  &lt;Setter Property="OverridesDefaultStyle" Value="true"/&gt;
  &lt;Setter Property="MinHeight" Value="23"/&gt;
  &lt;Setter Property="MinWidth" Value="75"/&gt;
  &lt;Setter Property="Template"&gt;
      &lt;Setter.Value&gt;
      &lt;ControlTemplate TargetType="{x:Type Rectangle}"&gt;
          &lt;Border x:Name="MyBorder"  
                 CornerRadius="2" 
                 BorderThickness="2"
                 Background="Gold"
                 BorderBrush="Gray"&gt;
          &lt;ContentPresenter Margin="2"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               RecognizesAccessKey="True"/&gt;
          &lt;/Border&gt;
          &lt;ControlTemplate.Triggers&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineAvailable"&gt;
              &lt;Setter TargetName="MyBorder" Property="Background" Value="Gold"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineIdle"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="Silver"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineBusy"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="Green"/&gt;
          &lt;/Trigger&gt;
          &lt;Trigger Property="View:DirectLineButton.State" Value="DirectLineCalled"&gt;
              &lt;Setter  TargetName="MyBorder" Property="Background" Value="LightBlue"/&gt;
          &lt;/Trigger&gt;
          &lt;/ControlTemplate.Triggers&gt;
      &lt;/ControlTemplate&gt;
      &lt;/Setter.Value&gt;
  &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>