我想在使用WPF创建的页面中隐藏导航栏。我尝试了ShowsNavigationUI = false
,但控件仍然显示。
我想在使用WPF创建的页面中隐藏导航栏。我尝试了ShowsNavigationUI = false
,但控件仍然显示。
使用NavigationUIVisibility
属性,向页面容器指定不需要导航栏。
<Frame NavigationUIVisibility="Hidden" Panel.ZIndex="1" ... />
这是一个非常简单的实现。
<Frame x:Name="_FrameName" NavigationUIVisibility="Hidden" />
在Page
上设置ShowsNavigationUI=False
应该可以实现。但是,似乎存在一个错误,会导致在至少一系列事件中失败:
Page
已经在NavigationWindow
中。Page
被导航离开并再次返回。可能还有其他我尚未遇到的场景会导致它失败。
为了使其完全可靠地工作,我会完全忽略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>
NavigationUIVisibility="Hidden"
即可。 - Or Betzalel<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>
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;
}
以上代码仅适用于导航窗口,而我正在使用普通的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>
最简单的方法之一是在您的cs文件构造函数中添加: ShowsNavigationUI = false; 在InitializeComponent()下方。
<Window>
<Grid >
<Frame x:Name="LeftFrame" NavigationUIVisibility="Hidden"/>
<Frame x:Name="RightFrame" NavigationUIVisibility="Hidden"/>
</Grid>
</Window>
ContentFrame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
ContentFrame是在XAML中定义的框架名称。
<Frame x:Name="ContentFrame" />
NavigationUIVisibility="Visible"
就可以解决问题。 - GeertVc