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# 中消除搜索框?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以使用
ContentControl
的样式和隐藏它的触发器来执行类似于 Cheeso 的回答 的操作当名称为PART_FindToolBarHost
时。You can do something similar to Cheeso's answer with a style for
ContentControl
and a trigger to hide it when the name isPART_FindToolBarHost
.Vlad 的回答引导了我了解如何以编程方式抓取包含查找工具栏的 ContentControl。我真的不想为 DocumentViewer 编写一个全新的模板;我只想更改(隐藏)一个控件。这将问题简化为如何检索通过模板应用的控件?。
这是我发现的:
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:
正如 Vlad 指出的,您可以替换控制模板。不幸的是,MSDN 上提供的控件模板并不是
DocumentViewer
控件使用的真正控件模板。以下是通过在PART_FindToolBarHost
上设置Visibility="Collapsed"
修改为隐藏搜索栏的正确模板:您需要添加对
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 settingVisibility="Collapsed"
onPART_FindToolBarHost
:You need to add a reference to
PresentationUI.dll
. This assembly is located in the folder%WINDIR%\Microsoft.NET\Framework\v4.0.30319\WPF
.您可以为其替换控件模板。供您参考:默认的
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.aspxThe search toolbar's name is
PART_FindToolBarHost
, so you can also just assign itsVisibility
toCollapsed
.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).
为了让 Cheeso 的答案在构造函数中工作,我必须添加:
否则 cc 输出为 null。请参阅此处的答案
In order to get Cheeso's answer to work in the constructor I had to add:
otherwise cc comes out null. See the answer here
您确定需要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.