共用方式為


Window ControlTemplate 範例

更新:2007 年 11 月

Windows Presentation Foundation (WPF) 中的控制項具有 ControlTemplate,其中包含該控制項的視覺化樹狀結構。您可以透過修改控制項的 ControlTemplate,變更該控制項的結構和外觀。您無法只取代控制項的視覺化樹狀結構的某一部分;若要變更控制項的視覺化樹狀結構,您必須將控制項的 Template 屬性設定為完整的新 ControlTemplate

本主題將說明 WPFWindow 控制項的 ControlTemplate

這個主題包含下列章節。

  • 必要條件
  • Windows ControlTemplate 範例
  • 相關主題

必要條件

若要執行本主題中的範例,您應該了解如何撰寫 WPF 應用程式。如需詳細資訊,請參閱 Windows Presentation Foundation 使用者入門。您也應該了解樣式在 WPF 中的使用方式。如需詳細資訊,請參閱設定樣式和範本

Windows ControlTemplate 範例

雖然這個範例包含 WindowControlTemplate 中預設定義的所有項目,但是您應該將特定值視為範例。

<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>

前述範例使用了下列資源。

<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" />

如需完整範例,請參閱使用 ControlTemplates 設定樣式範例

請參閱

概念

NavigationWindow ControlTemplate 範例

設計可設定樣式控制項的方針

其他資源

ControlTemplate 範例