WPF:如何删除文档查看器中的搜索框?

时间:2010-02-24 00:01:27

标签: 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>

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

7 个答案:

答案 0 :(得分:14)

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 :(得分:11)

您可以使用ContentControl的样式执行与Cheeso's answer类似的操作,并在名称为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>

答案 2 :(得分:4)

您可以替换它的控件模板。供您参考:默认DocumentViewer的控件模板位于:http://msdn.microsoft.com/en-us/library/aa970452.aspx

搜索工具栏的名称为PART_FindToolBarHost,因此您也可以将其Visibility分配给Collapsed


编辑:
正如@Martin的评论所暗示的那样,MSDN中的控件模板(上面引用)并不完全正确。提取默认情况下在WPF中实际使用的模板的更好方法是使用Blend(在上下文菜单中编辑控件模板,如果我没有记错的话)。

答案 3 :(得分:4)

正如弗拉德指出的那样,你可以替换控制模板。遗憾的是,MSDN上可用的控件模板不是DocumentViewer控件使用的真实控件模板。通过在Visibility="Collapsed"上设置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>

您需要添加对PresentationUI.dll的引用。此程序集位于文件夹%WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF

答案 4 :(得分:2)

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

dv1.ApplyTemplate();

否则cc出现null。请参阅答案here

答案 5 :(得分:1)

Popup-View

答案 6 :(得分:0)

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