Partager via


Window ControlTemplate Example

Controls in Windows Presentation Foundation (WPF) have a ControlTemplate that contains the visual tree of that control. You can change the structure and appearance of a control by modifying the ControlTemplate of that control. There is no way to replace only part of the visual tree of a control; to change the visual tree of a control you must set the Template property of the control to its new and complete ControlTemplate.

This topic shows the ControlTemplate of the WPF Window control.

This topic contains the following sections.

  • Prerequisites
  • Window ControlTemplate Example
  • Related Topics

Prerequisites

To run the examples in this topic, you should understand how to write WPF applications. For more information, see Get Started Using Windows Presentation Foundation. You should also understand how styles are used in WPF. For more information, see Styling and Templating.

Window ControlTemplate Example

While this example contains all of the elements that are defined in the ControlTemplate of a Window by default, the specific values should be thought of as examples.

<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}">
  <Setter Property="SnapsToDevicePixels" Value="true"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Window}">
        <Grid Background="{StaticResource WindowBackgroundBrush}">
          <AdornerDecorator>
            <ContentPresenter/>
          </AdornerDecorator>
          <ResizeGrip x:Name="WindowResizeGrip"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Visibility="Collapsed" 
            IsTabStop="false"/>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property="ResizeMode" Value="CanResizeWithGrip">
            <Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The above example uses the following resources:

<Style x:Key="{x:Type ResizeGrip}" TargetType="{x:Type ResizeGrip}">
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ResizeGrip}">
        <Border Background="Transparent"
          SnapsToDevicePixels="True"
          Width="16"
          Height="16">
          <Rectangle Margin="2">
            <Rectangle.Fill>
              <DrawingBrush Viewport="0,0,4,4" 
                ViewportUnits="Absolute"
                Viewbox="0,0,8,8"
                ViewboxUnits="Absolute"
                TileMode="Tile">
                <DrawingBrush.Drawing>
                  <DrawingGroup>
                    <DrawingGroup.Children>
                      <GeometryDrawing Brush="#AAA"
                        Geometry="M 4 4 L 4 8 L 8 8 L 8 4 z"/>
                    </DrawingGroup.Children>
                  </DrawingGroup>
                </DrawingBrush.Drawing>
              </DrawingBrush>
            </Rectangle.Fill>
          </Rectangle>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

  ...

<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />

For the complete sample, see Styling with ControlTemplates Sample.

See Also

Concepts

NavigationWindow ControlTemplate Example
Guidelines for Designing Stylable Controls

Other Resources

ControlTemplate Examples