WPF:如何在DocumentViewer中移除搜索框?

16

我的XAML代码如下:

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

我该如何在XAML或C#中消除搜索框?

7个回答

20

您可以通过为ContentControl设置样式并使用触发器在名称为PART_FindToolBarHost时隐藏它来实现类似于Cheeso的答案的操作。

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

15

Vlad's answer启发我去查看如何以编程方式抓取包含查找工具栏的ContentControl。我并不想编写一个全新的DocumentViewer模板;我只想更改(隐藏)其中一个控件。这就将问题简化为如何检索通过模板应用的控件?
以下是我的发现:

  Window window = ... ; 
  DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(window, "dv1") as DocumentViewer;
  ContentControl cc = dv1.Template.FindName("PART_FindToolBarHost", dv1) as ContentControl;
  cc.Visibility = Visibility.Collapsed;

4
+1. 但是:为了在窗口显示之前让它正常工作且不使用 "findLogicalNode",需要先使用 Quarkonium response 中的代码:dv1.ApplyTemplate(); - JYL

6

正如Vlad所指出的那样,您可以替换控件模板。不幸的是,MSDN上提供的控件模板并不是DocumentViewer控件实际使用的真正控件模板。这里是正确的模板,通过在PART_FindToolBarHost上设置Visibility="Collapsed"来隐藏搜索栏:

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

您需要添加对 PresentationUI.dll 的引用。此程序集位于文件夹 %WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF 中,需要注意的是,请确保已经添加了该文件夹。

4
您可以替换控件模板。供您参考:默认的DocumentViewer的控件模板在此处:http://msdn.microsoft.com/en-us/library/aa970452.aspx 搜索工具栏的名称为PART_FindToolBarHost,因此您也可以将其Visibility分配为Collapsed
编辑:
正如@Martin的评论所建议的那样,MSDN中的控件模板(上面引用的)并不完全正确。提取实际上默认在WPF中使用的模板更好的方法是使用Blend(如果我没有记错,则在上下文菜单中编辑控件模板)。

1
这是“正确”的解决方案,但存在一个错误,即您将获得设计模式(而不是运行时)异常“'Zoom'不是属性'Command'的有效值”。有关更多信息,请参见http://connect.microsoft.com/VisualStudio/feedback/details/566538/document-viewer-controltemplate。 - Martin Liversage

2
为了让Cheeso的答案在构造函数中起作用,我不得不添加以下内容:
dv1.ApplyTemplate();

否则cc将会为空。请参考这里的回答。

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>

0

你确定需要一个DocumentViewer吗?你可以使用FlowDocumentScrollViewer,或者如果你喜欢分页或多列显示,你可以使用FlowDocumentPageViewer


我想要一个DocumentViewer,因为我的目标是生成打印预览,而XpsDocument可以自动分页视觉内容。虽然我可以使用FDSV和其他自定义代码来实现这一点,但是...我宁愿偷懒。 - Cheeso
你让我想知道如何为FlowDocuments做打印预览......顺便说一下,从《Pro WPF in C# 2008》看起来,你需要将流文档写成XPS文件,然后再读取它(作为固定文档),最后在DocumentViewer中显示它...哇! - RAL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接