如何在 WPF 页面中隐藏导航栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3059650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:52:06  来源:igfitidea点击:

How to hide the navigation bar in a WPF page

wpfxamluinavigationbar

提问by Geeth

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.

我想在使用 WPF 创建的页面中隐藏导航栏。我试过了ShowsNavigationUI = false,但它仍然显示控件。

采纳答案by Ray Burns

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

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

  1. Page is already in NavigationWindow when this is set
  2. Page is navigated away and back again
  1. 设置时页面已经在 NavigationWindow 中
  2. 页面被导航离开并再次返回

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.

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

Here is how this can be done in your Page constructor:

以下是如何在您的 Page 构造函数中完成此操作:

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.

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

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:

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

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

回答by SiwachGaurav

just tell in Your page Container , you want navigation bar or not, using NavigationUIVisibility property.

只需在您的页面 Container 中使用 NavigationUIVisibility 属性告诉您是否需要导航栏。

<Frame Margin="173,41,1,28" Name="frmPageContainer" NavigationUIVisibility="Hidden" Panel.ZIndex="1" >

回答by Moacir Kurmann

It's a very easy implementation.

这是一个非常简单的实现。

<Frame x:Name="_FrameName" NavigationUIVisibility="Hidden" />

回答by Rana Ian

If you're using a Frameyou 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.

如果您使用的是框架,您可以更改框架的默认样式以移除导航按钮(如下所示)。可以对 NavigationWindow 执行相同的方法。我最初尝试设置 Page.ShowsNavigationUI 并没有效果。只需将以下样式添加到 ResourceDictionary 中,它就可以正常工作。

<Style TargetType="{x:Type Frame}">
  <Setter Property="Control.Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Frame}">
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}">
          <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Name="PART_FrameCP" />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

回答by Preston

This one I found really easy. In your MainWindow, do this:

这个我觉得很简单。在您的主窗口中,执行以下操作:

public MainWindow()
   public partial class MainWindow : NavigationWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            ShowsNavigationUI = false;
        }
    }
}

And if you have an event on button click to open a new page, just do this:

如果您有按钮单击以打开新页面的事件,请执行以下操作:

private void btnEndUserSearch_Click(object sender, RoutedEventArgs e)
{
            EndUser EndUserSearchPage = new EndUser();
            this.NavigationService.Navigate(EndUserSearchPage);
            EndUserSearchPage.ShowsNavigationUI = false;
}

回答by Anton Andreev

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.

以上仅适用于导航窗口,但我使用的是普通 WPF 窗口。有人说这些比导航窗口更好。我正在使用 DockPanel 来托管我的页面。我的解决方案为 DockPanel 创建了一个新模板,只是不添加按钮或隐藏它们(请参阅 StackPanel Visibility="Hidden")。它工作得很好。

<DockPanel>    
    <Frame x:Name="_mainFrame">
    <Frame.Template>

        <ControlTemplate TargetType="Frame">
            <DockPanel Margin="7">
                <StackPanel Visibility="Hidden"
                    Margin="0"
                    Orientation="Horizontal"
                    DockPanel.Dock="Top"
                    >
                    <!--<Button
                        Content="Avast! Go back!" 
                        Command="{x:Static NavigationCommands.BrowseBack}" 
                        IsEnabled="{TemplateBinding CanGoBack}" 
                        />
                    <Button 
                        Content="Forward you dogs!" 
                        Command="{x:Static NavigationCommands.BrowseForward}" 
                        IsEnabled="{TemplateBinding CanGoForward}" 
                        />-->
                </StackPanel>

               <Border>
                    <ContentPresenter />
               </Border>
            </DockPanel>
        </ControlTemplate>

        </Frame.Template>
    </Frame>
</DockPanel>

回答by Sachini Witharana

One of the easiest and simplest way is to add: ShowsNavigationUI = false; in your cs file constructor under InitializeComponent();

最简单最简单的方法之一是添加: ShowsNavigationUI = false; 在 InitializeComponent() 下的 cs 文件构造函数中;

回答by Epirocks

On the NavigationWindow itself I use ShowsNavigationUI="False"

在 NavigationWindow 本身我使用 ShowsNavigationUI="False"

回答by XtraSimplicity

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.

每当我动态更改框架的 Content 属性时,我都会遇到这个问题,并通过在我的 click() 事件中使用以下代码来解决它。

ContentFrame.NavigationUIVisibility = NavigationUIVisibility.Hidden;

Where ContentFrame is the name of the frame, as defined in XAML. i.e.

其中 ContentFrame 是框架的名称,如 XAML 中所定义。IE

<Frame x:Name="ContentFrame"  />