如何在 WPF 页面中隐藏导航栏 [英] How to hide the navigation bar in a WPF page

查看:129
本文介绍了如何在 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.

推荐答案

Page 上设置 ShowsNavigationUI=False 应该这样做.然而,似乎确实存在一个错误,它会导致至少在一个事件序列中失败:

Setting ShowsNavigationUI=False on a Page 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:

  1. 页面已经在 NavigationWindow 设置时
  2. 页面被导航离开并再次返回

可能还有其他我尚未遇到的情况导致它失败.

There may be other scenarios I haven't run into yet that make it fail.

为了让它完全可靠地工作,我所做的是完全忽略 Page.ShowsNavigationUI 属性并将其设置在 NavigationWindow 上.这似乎是完全可靠的.

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:

Dispatcher.BeginInvoke(ApplicationPriority.Render, new Action(() =>
{
  var navWindow = Window.GetWindow(this) as NavigationWindow;
  if(navWindow!=null) navWindow.ShowsNavigationUI = false;
}));

如果这样做,请记住不要在任何 Page 对象上设置 ShowsNavigationUI.

If you do this, remember not to set ShowsNavigationUI on any Page object.

仅供参考,您还可以通过更改其 ControlTemplate 以任何您喜欢的方式重新设计您的 NavigationWindow.例如,这将删除除实际页面内容之外的所有内容:

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:

  <Style TargetType="{x:Type NavigationWindow}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type NavigationWindow}">

          <AdornerDecorator>
            <ContentPresenter Name="PART_NavWinCP" 
                              ClipToBounds="true"/>
          </AdornerDecorator>
          
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

这篇关于如何在 WPF 页面中隐藏导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆