首页 > 解决方案 > 以编程方式选择 NavigationView 溢出列表中的项目

问题描述

当我尝试以编程方式选择 a 的“溢出列表”中的项目时NavigationView,出现以下错误:

参数不正确。

容器'

下图是一个示例,我尝试以编程方式选择菜单项 3:


导航视图溢出

var nextItem = dataSource.indexOf(menuItem3); NavView.SelectedItem = nextItem;


选择未溢出的项目时效果很好。

标签: c#uwpuwp-xaml

解决方案


参数不正确。容器'

为了解释这种行为,我们需要检查Generic.xaml文件NavigationView中的样式。

<Button 
    x:Name="TopNavOverflowButton"
    Grid.Column="4"

    Content="More"
    Style="{StaticResource NavigationViewOverflowButtonStyleWhenPaneOnTop}"
    Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.OverflowButtonVisibility}">

    <Button.Flyout>
        <Flyout Placement="Bottom">
            <Flyout.FlyoutPresenterStyle>
                <Style TargetType="FlyoutPresenter">
                    <Setter Property="Padding" Value="0,8" />
                    <!-- Set negative top margin to make the flyout align exactly with the button -->
                    <Setter Property="Margin" Value="0,-4,0,0" />
                </Style>
            </Flyout.FlyoutPresenterStyle>
            <NavigationViewList x:Name="TopNavMenuItemsOverflowHost" ItemTemplate="{TemplateBinding MenuItemTemplate}" ItemTemplateSelector="{TemplateBinding MenuItemTemplateSelector}" ItemContainerStyle="{TemplateBinding MenuItemContainerStyle}" ItemContainerStyleSelector="{TemplateBinding MenuItemContainerStyleSelector}" SingleSelectionFollowsFocus="False" IsItemClickEnabled="True">
                <ListView.ItemContainerTransitions>
                    <TransitionCollection />
                </ListView.ItemContainerTransitions>
            </NavigationViewList>
        </Flyout>
    </Button.Flyout>
</Button>

如您所见,更多项目存储在TopNavMenuItemsOverflowHost NavigationViewList中。它的容器是Flyout. Flyout 是延迟加载控制。如果它没有弹出,它的内容将不会被加载。这就是容器丢失的原因。

我认为在TopNavMenuItemsOverflowHost. 如果您想正确选择项目,请确保足够的窗口宽度可以使所有项目显示在NavigationView.


推荐阅读