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

发布于 2024-08-22 16:10:31 字数 482 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尤怨 2024-08-29 16:10:31

您可以使用 ContentControl 的样式和隐藏它的触发器来执行类似于 Cheeso 的回答 的操作当名称为 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>

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>
爱殇璃 2024-08-29 16:10:31

Vlad 的回答引导了我了解如何以编程方式抓取包含查找工具栏的 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;

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;
拥有 2024-08-29 16:10:31

正如 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 中。

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.

好多鱼好多余 2024-08-29 16:10:31

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

搜索工具栏的名称是 PART_FindToolBarHost,因此您也可以只分配其 可见性变为折叠


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

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

寄与心 2024-08-29 16:10:31

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

dv1.ApplyTemplate();

否则 cc 输出为 null。请参阅此处的答案

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

何时共饮酒 2024-08-29 16:10:31
 <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>
 <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>
空心空情空意 2024-08-29 16:10:31

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文