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

发布于 2024-09-06 15:19:11 字数 82 浏览 5 评论 0原文

我想在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

垂暮老矣 2024-09-13 15:19:11

指定到页面Container,本意是没有导航栏,使用
NavigationUIVisibility 属性。

<Frame NavigationUIVisibility="Hidden" Panel.ZIndex="1" ... />

Specify to the page Container, the intention to not have a navigation bar, using
NavigationUIVisibility property.

<Frame NavigationUIVisibility="Hidden" Panel.ZIndex="1" ... />
漆黑的白昼 2024-09-13 15:19:11

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

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

It's a very easy implementation.

<Frame x:Name="_FrameName" NavigationUIVisibility="Hidden" />
在风中等你 2024-09-13 15:19:11

Page 上设置 ShowsNavigationUI=False 应该可以做到这一点。然而,似乎确实存在一个错误,该错误将导致至少在一系列事件中失败:

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

可能有我还没有遇到其他导致失败的情况。

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

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

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

如果执行此操作,请记住不要在任何 Page 对象上设置 ShowsNavigationUI。

仅供参考,您还可以通过更改其 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>

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>
北渚 2024-09-13 15:19:11

如果您使用框架,则可以更改框架的默认样式以删除导航按钮(如下所示)。可以对导航窗口执行相同的方法。我最初尝试设置 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>

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.

<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>
终陌 2024-09-13 15:19:11

我发现这个非常简单。在您的主窗口中,执行以下操作:

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

            ShowsNavigationUI = false;
        }
    }
}

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

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

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;
}
演出会有结束 2024-09-13 15:19:11

上面仅适用于导航窗口,但我使用的是普通的 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>

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.

<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>
无尽的现实 2024-09-13 15:19:11

最简单的方法之一是添加:
显示导航UI = false;
在你的cs文件构造函数中InitializeComponent();

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

感情废物 2024-09-13 15:19:11

对于初学者,您可以像这样简单地更改 MainWindow.xaml 中的代码。

    <Window>
       <Grid >
          <Frame x:Name="LeftFrame" NavigationUIVisibility="Hidden"/>
          <Frame x:Name="RightFrame" NavigationUIVisibility="Hidden"/>
       </Grid>
    </Window>

For beginners, you can simply change code at MainWindow.xaml like this.

    <Window>
       <Grid >
          <Frame x:Name="LeftFrame" NavigationUIVisibility="Hidden"/>
          <Frame x:Name="RightFrame" NavigationUIVisibility="Hidden"/>
       </Grid>
    </Window>
梦明 2024-09-13 15:19:11

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

ContentFrame.NavigationUIVisibility = NavigationUIVisibility.Hidden;

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

<Frame x:Name="ContentFrame"  />

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.

ContentFrame.NavigationUIVisibility = NavigationUIVisibility.Hidden;

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

<Frame x:Name="ContentFrame"  />
攒一口袋星星 2024-09-13 15:19:11

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

On the NavigationWindow itself I use ShowsNavigationUI="False"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文