WPF:DocumentViewer の検索ボックスを削除するにはどうすればよいですか?
-
22-09-2019 - |
質問
私の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# で検索ボックスを削除するにはどうすればよいですか?
解決
ヴラドの答えプログラムで検索ツールバーを保持している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;
他のヒント
あなたは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>
としてヴラドは、あなたがコントロールテンプレートを置き換えることができます指摘しました。残念ながら、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
に位置しています。
あなたはそれのためにコントロールテンプレートを置き換えることができます。あなたの参考のために:デフォルトのDocumentViewer
のコントロールテンプレートはここにある:ます。http:// MSDN。 microsoft.com/en-us/library/aa970452.aspxする
あなたもちょうどPART_FindToolBarHost
にそのVisibility
を割り当てることができますので、検索ツールバーの名前は、Collapsed
です。
編集:
@Martinからコメントが示唆するように、(上記参照)MSDNでコントロールテンプレートは、完全に正確ではありません。実際には、デフォルトではWPFで使用されているテンプレートを抽出するためのより良い方法(私は間違っていないよ場合は、コンテキストメニューの[編集]コントロールテンプレート)ブレンドを使用することでしょう。
コンストラクタで仕事にCheesoの答えを得るために、私は追加する必要がありました。
dv1.ApplyTemplate();
それ以外のCCはヌルを出てきます。
の答えはここにhref="https://stackoverflow.com/a/2286693/1196481"> <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>
本当に必要ですか? ドキュメントビューア?を使用できます フロードキュメントスクロールビューア 代わりに、またはページネーションや複数列表示が好きな場合は、 フロードキュメントページビューア.