15

My XAML code is like this:

<Window
    xmlns                 ='http://schemas.microsoft.com/netfx/2007/xaml/presentation'
    xmlns:x               ='http://schemas.microsoft.com/winfx/2006/xaml'
    Title                 ='Print Preview - More stuff here'
    Height                ='200'
    Width                 ='300'
    WindowStartupLocation ='CenterOwner'>
    <DocumentViewer Name='dv1' ... />
</Window>

How can I, in XAML or in C#, eliminate the search box?

Cheeso
  • 189,189
  • 101
  • 473
  • 713

7 Answers7

19

You can do something similar to Cheeso's answer with a style for ContentControl and a trigger to hide it when the name is PART_FindToolBarHost.

<DocumentViewer>
  <DocumentViewer.Resources>
    <Style TargetType="ContentControl">
      <Style.Triggers>
        <Trigger Property="Name" Value="PART_FindToolBarHost">
          <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </DocumentViewer.Resources>
</DocumentViewer>
Community
  • 1
  • 1
Garrett
  • 649
  • 6
  • 7
15

Vlad's answer led me to look at how to programmatically grab the ContentControl that holds the find toolbar. I didn't really want to write an entirely new template for the DocumentViewer; I wanted to change (hide) only one control. That reduced the problem to how to retrieve a control that is applied via a template?.
Here's what I figured out:

  Window window = ... ; 
  DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(window, "dv1") as DocumentViewer;
  ContentControl cc = dv1.Template.FindName("PART_FindToolBarHost", dv1) as ContentControl;
  cc.Visibility = Visibility.Collapsed;
Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • 4
    +1. But: in order to make this working without the "findLogicalNode" and before the window is displayed, it needs the code of [Quarkonium response](http://stackoverflow.com/a/9270218/218873) first : `dv1.ApplyTemplate();` – JYL Oct 08 '12 at 16:20
6

As Vlad pointed out you can replace the control template. Unfortunately, the control template available on MSDN is not the real control template used by the DocumentViewer control. Here is the correct template modified to hide the search bar by setting Visibility="Collapsed" on PART_FindToolBarHost:

<!-- DocumentViewer style with hidden search bar. -->
<Style TargetType="{x:Type DocumentViewer}" xmlns:Documents="clr-namespace:System.Windows.Documents;assembly=PresentationUI">
  <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 ResourceId=PUIDocumentViewerContextMenu, TypeInTargetAssembly={x:Type Documents:PresentationUIStyleResources}}}"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type DocumentViewer}">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Focusable="False">
          <Grid Background="{TemplateBinding Background}" KeyboardNavigation.TabNavigation="Local">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ContentControl Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="0" Style="{DynamicResource {ComponentResourceKey ResourceId=PUIDocumentViewerToolBarStyleKey, TypeInTargetAssembly={x:Type Documents:PresentationUIStyleResources}}}" TabIndex="0"/>
            <ScrollViewer x:Name="PART_ContentHost" CanContentScroll="true" Grid.Column="0" Focusable="{TemplateBinding Focusable}" HorizontalScrollBarVisibility="Auto" IsTabStop="true" Grid.Row="1" TabIndex="1"/>
            <DockPanel Grid.Row="1">
              <FrameworkElement DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
              <Rectangle Height="10" Visibility="Visible" VerticalAlignment="top">
                <Rectangle.Fill>
                  <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                    <LinearGradientBrush.GradientStops>
                      <GradientStopCollection>
                        <GradientStop Color="#66000000" Offset="0"/>
                        <GradientStop Color="Transparent" Offset="1"/>
                      </GradientStopCollection>
                    </LinearGradientBrush.GradientStops>
                  </LinearGradientBrush>
                </Rectangle.Fill>
              </Rectangle>
            </DockPanel>
            <ContentControl x:Name="PART_FindToolBarHost" Grid.Column="0" Focusable="{TemplateBinding Focusable}" Grid.Row="2" TabIndex="2" Visibility="Collapsed"/>
          </Grid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

You need to add a reference to PresentationUI.dll. This assembly is located in the folder %WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
4

You can replace a control template for it. For your reference: the default DocumentViewer's control template is here: http://msdn.microsoft.com/en-us/library/aa970452.aspx

The search toolbar's name is PART_FindToolBarHost, so you can also just assign its Visibility to Collapsed.


Edit:
As the comment from @Martin suggests, the control template in MSDN (referenced above) is not fully correct. A better way to extract a template which is actually used in WPF by default would be using Blend (Edit Control Template in the context menu, if I am not mistaken).

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 1
    This is the 'right' solution except there is a bug where you will get the design mode (not run time) exception `'Zoom' is not a valid value for property 'Command'.`. See http://connect.microsoft.com/VisualStudio/feedback/details/566538/document-viewer-controltemplate for more information. – Martin Liversage Jan 17 '12 at 19:46
2
 <DocumentViewer>
     <DocumentViewer.Resources>
         <!-- Toolbar -->          
         <Style TargetType="ToolBar">
             <Setter Property="Visibility" Value="Collapsed" />
         </Style>
          <!-- Search -->
         <Style TargetType="ContentControl">
             <Setter Property="Visibility" Value="Collapsed" />
         </Style>
     </DocumentViewer.Resources>
</DocumentViewer>
juFo
  • 17,849
  • 10
  • 105
  • 142
2

In order to get Cheeso's answer to work in the constructor I had to add:

dv1.ApplyTemplate();

otherwise cc comes out null. See the answer here

Community
  • 1
  • 1
quarkonium
  • 322
  • 3
  • 13
0

Are you sure you need a DocumentViewer? You could use a FlowDocumentScrollViewer instead, or if you like pagination or multi-column display, you could use a FlowDocumentPageViewer.

RAL
  • 917
  • 8
  • 19
  • I want a DocumentViewer because my goal is to produce print preview, and the XpsDocument is the thing that paginates visuals automatically. I could do this with a FDSV and some other custom code, but... I'd rather do the lazy thing. – Cheeso Feb 24 '10 at 01:44
  • you got me wondering how you do print preview for FlowDocuments...fwiw, from "Pro WPF in C# 2008" looks like you write the flow doc out as an XPS file then read it back in (as a fixed document) and finally display it in a DocumentViewer...wow! – RAL Feb 24 '10 at 05:12