【问题标题】:WPF: How can I remove the searchbox in a DocumentViewer?WPF:如何删除 DocumentViewer 中的搜索框?
【发布时间】:2010-02-24 00:01:27
【问题描述】:

我的 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# 中消除搜索框?

【问题讨论】:

    标签: wpf documentviewer


    【解决方案1】:

    您可以使用ContentControl 的样式和当名称为PART_FindToolBarHost 时隐藏它的触发器来执行类似于Cheeso's answer 的操作。

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

    【讨论】:

      【解决方案2】:

      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;
      

      【讨论】:

      • +1。但是:为了使这个在没有“findLogicalNode”的情况下工作并且在窗口显示之前,它首先需要Quarkonium response的代码:dv1.ApplyTemplate();
      【解决方案3】:

      正如 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(如果我没记错的话,请在上下文菜单中编辑控件模板)。

        【讨论】:

        【解决方案5】:

        为了让 Cheeso 的答案在构造函数中起作用,我必须添加:

        dv1.ApplyTemplate();
        

        否则 cc 会返回 null。看答案here

        【讨论】:

          【解决方案6】:
           <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>
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

            • 我想要一个 DocumentViewer,因为我的目标是生成打印预览,而 XpsDocument 是自动分页视觉效果的东西。我可以使用 FDSV 和其他一些自定义代码来做到这一点,但是......我宁愿做懒惰的事情。
            • 您让我想知道您如何为 FlowDocuments 进行打印预览...fwiw,来自“C# 2008 中的 Pro WPF”看起来您将流文档写成 XPS 文件然后将其读回(作为一个固定的文档),最后在 DocumentViewer 中显示它......哇!
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-23
            • 1970-01-01
            • 1970-01-01
            • 2016-06-19
            • 1970-01-01
            相关资源
            最近更新 更多