如何在WPF页面中隐藏导航栏
我想在使用 WPF 创建的页面中隐藏导航栏。我已尝试 ShowsNavigationUI = false
,但它仍然显示控件。
I want to hide the navigation bar in a page created using WPF. I have tried ShowsNavigationUI = false
, but it is still displaying the control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
指定到页面Container,本意是没有导航栏,使用
NavigationUIVisibility
属性。Specify to the page Container, the intention to not have a navigation bar, using
NavigationUIVisibility
property.这是一个非常简单的实现。
It's a very easy implementation.
在
Page
上设置ShowsNavigationUI=False
应该可以做到这一点。然而,似乎确实存在一个错误,该错误将导致至少在一系列事件中失败:NavigationWindow
中可能有我还没有遇到其他导致失败的情况。
为了使其完全可靠地工作,我所做的就是完全忽略 Page.ShowsNavigationUI 属性并将其设置在 NavigationWindow 上。这似乎是完全可靠的。
以下是如何在 Page 构造函数中完成此操作:
如果执行此操作,请记住不要在任何 Page 对象上设置 ShowsNavigationUI。
仅供参考,您还可以通过更改其 ControlTemplate 以任何您喜欢的方式重新设计您的 NavigationWindow。例如,这会删除除实际页面内容之外的所有内容:
Setting
ShowsNavigationUI=False
on aPage
ought to do it. There does seem to be a bug, however, that will cause this to fail in at least one sequence of events:NavigationWindow
when this is setThere may be other scenarios I haven't run into yet that make it fail.
To get this to work totally reliably, what I do is ignore the Page.ShowsNavigationUI property entirely and set it instead on NavigationWindow. This seems to be completely reliable.
Here is how this can be done in your Page constructor:
If you do this, remember not to set ShowsNavigationUI on any Page object.
FYI, you can also restyle your NavigationWindow any way you like by changing its ControlTemplate. For example this removes everything but the actual page content:
如果您使用框架,则可以更改框架的默认样式以删除导航按钮(如下所示)。可以对导航窗口执行相同的方法。我最初尝试设置 Page.ShowsNavigationUI 但没有效果。只需将以下样式添加到 ResourceDictionary 即可正常工作。
If you're using a Frame you can change the Frame's default style to remove the navigation buttons (shown below). The same approach could be done for NavigationWindow. I originally tried setting Page.ShowsNavigationUI and it had no effect. Just add the below style to a ResourceDictionary and it works fine.
我发现这个非常简单。在您的主窗口中,执行以下操作:
如果您有一个单击按钮打开新页面的事件,只需执行以下操作:
This one I found really easy. In your MainWindow, do this:
And if you have an event on button click to open a new page, just do this:
上面仅适用于导航窗口,但我使用的是普通的 WPF 窗口。有人说这些比导航窗口更好。我正在使用 DockPanel 来托管我的页面。我的解决方案为 DockPanel 创建一个新模板,并且只是不添加按钮或将它们隐藏(请参阅 StackPanel Visibility="Hidden")。效果很好。
Above works only for Navigation windows, but I am using ordinary WPF windows. Some say these are better than Navigation windows. I am using DockPanel to host my pages. My solution creates a new template for the DockPanel and simply does not add buttons or makes them hidden (see StackPanel Visibility="Hidden"). It works nicely.
最简单的方法之一是添加:
显示导航UI = false;
在你的cs文件构造函数中InitializeComponent();
One of the easiest and simplest way is to add:
ShowsNavigationUI = false;
in your cs file constructor under InitializeComponent();
对于初学者,您可以像这样简单地更改 MainWindow.xaml 中的代码。
For beginners, you can simply change code at MainWindow.xaml like this.
每当我动态更改 Frame 的 Content 属性时,我都会遇到此问题,并通过在 click() 事件中使用以下代码来解决它。
其中 ContentFrame 是框架的名称,如 XAML 中所定义。 IE
I had this problem whenever I dynamically changed the Content property of a Frame, and solved it by using the following code in my click() event.
Where ContentFrame is the name of the frame, as defined in XAML. i.e.
在 NavigationWindow 本身上,我使用 ShowsNavigationUI="False"
On the NavigationWindow itself I use ShowsNavigationUI="False"