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

Che*_*eso 12 wpf documentviewer

我的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>
Run Code Online (Sandbox Code Playgroud)

如何在XAML或C#中删除搜索框?

Che*_*eso 14

弗拉德的回答让我看看如何以编程方式抓取包含查找工具栏的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;
Run Code Online (Sandbox Code Playgroud)

  • +1.但是:为了使这个工作没有"findLogicalNode"并且在窗口显示之前,它首先需要[Quarkonium响应]的代码(http://stackoverflow.com/a/9270218/218873):`dv1.ApplyTemplate ();` (4认同)

小智 13

您可以使用样式执行与Cheeso的答案类似的操作,ContentControl并在名称时触发隐藏它的触发器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>
Run Code Online (Sandbox Code Playgroud)


Mar*_*age 6

正如 Vlad 指出的那样,您可以替换控制模板。不幸的是,MSDN 上可用的控件模板并不是控件使用的真正控件模板DocumentViewer。这是通过设置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>
Run Code Online (Sandbox Code Playgroud)

您需要添加对PresentationUI.dll. 该程序集位于文件夹中%WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF