15

私の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# で検索ボックスを削除するにはどうすればよいですか?

4

7 に答える 7

18

名前が. _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>
于 2013-01-11T21:57:02.623 に答える
15

Vladの回答により、検索ツールバーを保持するContentControlをプログラムで取得する方法を検討することになりました。DocumentViewer用のまったく新しいテンプレートを書きたくありませんでした。1つのコントロールだけを変更(非表示)したかったのです。これにより、テンプレートを介して適用されるコントロールを取得する方法の問題が軽減されましたか?。
これが私が理解したことです:

  Window window = ... ; 
  DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(window, "dv1") as DocumentViewer;
  ContentControl cc = dv1.Template.FindName("PART_FindToolBarHost", dv1) as ContentControl;
  cc.Visibility = Visibility.Collapsed;
于 2010-02-24T02:28:55.550 に答える
6

Vlad が指摘したように、コントロール テンプレートを置き換えることができます。残念ながら、MSDN で入手できるコントロール テンプレートは、コントロールで使用される実際のコントロール テンプレートではありませんDocumentViewerVisibility="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>

への参照を追加する必要がありますPresentationUI.dll。このアセンブリはフォルダにあります%WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF

于 2012-01-17T20:07:42.017 に答える
4

コントロール テンプレートを置き換えることができます。参考までに、デフォルトDocumentViewerのコントロール テンプレートは次のとおりです: http://msdn.microsoft.com/en-us/library/aa970452.aspx

検索ツールバーの名前はPART_FindToolBarHostであるため、単に に割り当てることもできVisibilityますCollapsed


編集:
@Martin からのコメントが示唆するように、MSDN のコントロール テンプレート (上記参照) は完全には正しくありません。デフォルトでWPFで実際に使用されているテンプレートを抽出するより良い方法は、ブレンドを使用することです(私が間違っていなければ、コンテキストメニューのコントロールテンプレートの編集)。

于 2010-02-24T00:03:31.320 に答える
2

Cheeso の答えをコンストラクターで機能させるには、次を追加する必要がありました。

dv1.ApplyTemplate();

それ以外の場合、cc は null になります。答えはこちら

于 2012-02-14T00:43:12.503 に答える
0

本当にDocumentViewerが必要ですか? 代わりにFlowDocumentScrollViewerを使用できます。または、ページネーションまたは複数列の表示が必要な場合は、FlowDocumentPageViewerを使用できます。

于 2010-02-24T01:27:23.990 に答える