简体   繁体   English

“当窗格显示模式设置为“顶部”时,如何将 UWP NavigationView 控件中的导航视图项居中?”

[英]"How can I center a navigation view item in the UWP NavigationView control when the pane display mode is set to 'Top'?"

I am trying to create a navigation menu using the NavigationView control in UWP, and I want to center a navigation item when the pane display mode is set to 'Top'.我正在尝试使用 UWP 中的 NavigationView 控件创建导航菜单,当窗格显示模式设置为“顶部”时,我想将导航项居中。

I have tried using the HorizontalAlignment and VerticalAlignment properties, but they don't seem to have any effect.我试过使用 HorizontalAlignment 和 VerticalAlignment 属性,但它们似乎没有任何效果。 Can anyone provide an example of how to center a navigation item in NavigationView control when the pane display mode is set to 'Top'?谁能提供一个示例,说明当窗格显示模式设置为“顶部”时如何在 NavigationView 控件中居中导航项?

<Grid>
    <NavigationView PaneDisplayMode="Top"
                    HorizontalContentAlignment="Center">

        <NavigationView.MenuItems>
            <NavigationViewItem Content="Nav iTem"/>
            <NavigationViewItem Content="Nav iTem"/>
            <NavigationViewItem Content="Nav iTem"/>
        </NavigationView.MenuItems>
       
    </NavigationView>
</Grid>

You will need to modify the default style of the NavigationView to achieve what you want.您将需要修改NavigationView的默认样式以实现您想要的效果。 You could refer to @FrozenAssassine's answer here: https://stackoverflow.com/a/74861708/8443430 to create a copy of the default style of the NavigationView .您可以在此处参考@FrozenAssassine 的回答: https://stackoverflow.com/a/74861708/8443430以创建NavigationView默认样式的副本。 Then search for x:Name="TopNavGrid" and you will see the part of navigation items when in the top mode.然后搜索x:Name="TopNavGrid" ,您将在顶部模式下看到导航项的部分。

Part of the default style:部分默认样式:

<Grid x:Name="TopNavGrid" .....>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition x:Name="BackButtonPlaceholderOnTopNav" Width="{ThemeResource NavigationBackButtonWidth}"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition MinWidth="48" Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                          ... other controls..

                                    <NavigationViewList x:Name="TopNavMenuItemsHost"
                                                        Grid.Column="3"
                                                        HorizontalContentAlignment="Center"
                                                         .... other properties...
                                                         >
                                     
                                    </NavigationViewList>

The items are in a NavigationViewList control called TopNavMenuItemsHost and the TopNavMenuItemsHost is placed in a column of the TopNavGrid .这些项目位于名为TopNavMenuItemsHostNavigationViewList控件中, TopNavMenuItemsHost位于TopNavGrid的列中。 The width of the column is set to Auto which means the width depends on the content.列的宽度设置为自动,这意味着宽度取决于内容。 The column will be as big as the TopNavMenuItemsHost .该列将与TopNavMenuItemsHost一样大。 So you can't make the items placed in the center.所以你不能把物品放在中央。

To change the beahvior, please modify the ColumnDefinitions to the following:要更改行为,请将 ColumnDefinitions 修改为以下内容:

  <Grid.ColumnDefinitions>
                                        <ColumnDefinition x:Name="BackButtonPlaceholderOnTopNav" Width="{ThemeResource NavigationBackButtonWidth}"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition MinWidth="48" Width="48"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>

Make the ColumnDefinition of the TopNavMenuItemsHost bigger and then you will be able to set the HorizontalAlignment="Center" to the TopNavMenuItemsHost .使 TopNavMenuItemsHost 的TopNavMenuItemsHost更大,然后您就可以将 HorizontalAlignment HorizontalAlignment="Center"设置为TopNavMenuItemsHost

 <NavigationViewList x:Name="TopNavMenuItemsHost"
                                                        Grid.Column="3" 
                                        .... other properties...
                                                        
                                                        HorizontalAlignment="Center"
                                                        SingleSelectionFollowsFocus="{Binding TemplateSettings.SingleSelectionFollowsFocus, RelativeSource={RelativeSource Mode=TemplatedParent}}" 
                                                        ScrollViewer.VerticalScrollBarVisibility="Hidden" 
                                                        ScrollViewer.VerticalScrollMode="Disabled">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM