Compartir a través de


Ejemplo de ControlTemplate para DocumentViewer

Actualización: noviembre 2007

Los controles de Windows Presentation Foundation (WPF) tienen un objeto ControlTemplate que contiene el árbol visual de ese control. Puede cambiar la estructura y la apariencia de un control si modifica la ControlTemplate de ese control. No existe ningún modo de reemplazar únicamente una parte del árbol visual de un control; para modificar el árbol visual de un control, debe establecer la propiedad Template del control en su nuevo y completo objeto ControlTemplate.

En este tema se muestra el objeto ControlTemplate del control DocumentViewer de WPF.

Este tema contiene las secciones siguientes.

  • Requisitos previos
  • Ejemplo de ControlTemplate para DocumentViewer
  • Temas relacionados

Requisitos previos

Para ejecutar los ejemplos de este tema, debe saber cómo escribir aplicaciones de WPF. Para obtener más información, consulte Información general sobre Windows Presentation Foundation. También debe saber cómo se usan los estilos en WPF. Para obtener más información, consulte Aplicar estilos y plantillas.

Ejemplo de ControlTemplate para DocumentViewer

Aunque este ejemplo contiene todos los elementos que se definen en la ControlTemplate de un objeto DocumentViewer de manera predeterminada, los valores específicos deben tomarse como ejemplos.

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

En el ejemplo anterior se usa el recurso siguiente:

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

Para obtener el ejemplo completo, vea Ejemplo Styling with ControlTemplates.

Vea también

Conceptos

Instrucciones para el diseño de controles con estilos

Otros recursos

Ejemplos de ControlTemplate