Search code examples
wpfxamluinavigationbar

How to hide the navigation bar in a WPF page


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.


Solution

  • 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. Page is already in NavigationWindow when this is set
    2. Page is navigated away and back again

    There 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:

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

    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:

      <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>