Share via


DocumentViewer 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 DocumentViewer control.

This topic contains the following sections.

  • Prerequisites
  • DocumentViewer ControlTemplate Example
  • Related Topics

Prerequisites

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

DocumentViewer ControlTemplate Example

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

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

The preceding example uses the following resource:

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

For the complete sample, see Styling with ControlTemplates Sample.

See Also

Concepts

Guidelines for Designing Stylable Controls

Other Resources

ControlTemplate Examples