共用方式為


DocumentViewer ControlTemplate 範例

更新:2007 年 11 月

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

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

這個主題包含下列章節。

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

必要條件

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

DocumentViewer ControlTemplate 範例

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

<Style x:Key="{x:Type DocumentViewer}" TargetType="DocumentViewer">
  <Setter Property="Foreground"
          Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
  <Setter Property="Background"
          Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
  <Setter Property="ContextMenu"
          Value="{DynamicResource {ComponentResourceKey
          TypeInTargetAssembly={x:Type ui:PresentationUIStyleResources},
          ResourceId=PUIDocumentViewerContextMenu}}"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="DocumentViewer">
        <Border BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}" Focusable="False">
          <Grid Background="{StaticResource LightBrush}"
            KeyboardNavigation.TabNavigation="Local">
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ToolBar 
              ToolBarTray.IsLocked="True" 
              KeyboardNavigation.TabNavigation="Continue">
              <Button Command="ApplicationCommands.Print" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                Content="Print"/>
              <Button Command="ApplicationCommands.Copy" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                Content="Copy"/>
              <Separator />
              <Button Command="NavigationCommands.IncreaseZoom" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                Content="Zoom In"/>
              <Button Command="NavigationCommands.DecreaseZoom" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                Content="Zoom Out"/>
              <Separator />
              <Button Command="NavigationCommands.Zoom" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                CommandParameter="100.0" 
                Content="Actual Size" />
              <Button Command="DocumentViewer.FitToWidthCommand" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                Content="Fit to Width" />
              <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                CommandParameter="1" 
                Content="Whole Page"/>
              <Button Command="DocumentViewer.FitToMaxPagesAcrossCommand" 
                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                CommandParameter="2" 
                Content="Two Pages"/>
            </ToolBar>

            <ScrollViewer Grid.Row="1"
              CanContentScroll="true"
              HorizontalScrollBarVisibility="Auto"
              x:Name="PART_ContentHost"
              IsTabStop="true"/>

            <ContentControl Grid.Row="2"
              x:Name="PART_FindToolBarHost"/>
          </Grid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

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

<LinearGradientBrush x:Key="LightBrush" StartPoint="0,0" EndPoint="0,1">
  <GradientBrush.GradientStops>
    <GradientStopCollection>
      <GradientStop Color="#FFF" Offset="0.0"/>
      <GradientStop Color="#EEE" Offset="1.0"/>
    </GradientStopCollection>
  </GradientBrush.GradientStops>
</LinearGradientBrush>

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

請參閱

概念

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

其他資源

ControlTemplate 範例