简体   繁体   English

UWP-在SplitView.Pane中使用ListView

[英]UWP - Using ListView inside SplitView.Pane

I have currently a hamburger menu for my UWP app build using SplitView.Pane. 我目前使用SplitView.Pane为我的UWP应用构建一个汉堡菜单。

The problem with this implementation is, only the "Symbol" or <Button> element is actually clickable, not the text beside it in the pane. 这种实现方式的问题在于,实际上只有“ Symbol”或<Button>元素是可单击的,而窗格中它旁边的文本却不可单击。

I read on this forum and other tutorials that some have solved this problem by using a ListView, but in the GitHub samples I see that they have done this with CS instead of a simpler XAML implementation. 我在这个论坛和其他教程上读到了一些通过使用ListView解决了此问题的方法,但是在GitHub示例中,我看到他们使用CS而不是更简单的XAML实现来完成此操作。

Does anybody know how to do this in the XAML directly? 有人知道如何直接在XAML中执行此操作吗?

ListView is an ItemsControl , so it can contain a collection of items of any type. ListView是ItemsControl ,因此它可以包含任何类型的项目的集合。 To populate the view, add items to the Items collection, or set the ItemsSource property to a data source. 要填充视图,请将项目添加到Items集合,或将ItemsSource属性设置为数据源。

For more info, see ListView . 有关更多信息,请参见ListView

A common scenario is to bind to a collection of business objects. 常见方案是绑定到业务对象的集合。 In C# and Visual Basic, the generic ObservableCollection class is a good collection choice for data binding. 在C#和Visual Basic中,通用的ObservableCollection类是数据绑定的理想选择。 For more info, see Binding to a collection of items . 有关更多信息,请参见绑定到项目集合

But, we can also add ListViewItem to the ListView in XAML code. 但是,我们还可以添加ListViewItemListView在XAML代码。

For example: 例如:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <RelativePanel>
        <Button FontFamily="Segoe MDL2 Assets" FontSize="36" Content="&#xE700;" Click="Button_Click"></Button>
    </RelativePanel>
    <SplitView Grid.Row="1" Name="mySplitView" DisplayMode="CompactOverlay" OpenPaneLength="200" CompactPaneLength="56" HorizontalAlignment="Left">
        <SplitView.Pane>
            <ListView Name="MyListView"  SelectionChanged="ListView_SelectionChanged">
                <ListView.Items>
                    <ListViewItem Name="FristItem">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock  FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE170;"></TextBlock>
                            <TextBlock Margin="20,0,0,0" Text="Click" FontSize="36"></TextBlock>
                        </StackPanel>
                    </ListViewItem>
                    <ListViewItem Name="SecondItem">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock  FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE171;"></TextBlock>
                            <TextBlock Margin="20,0,0,0" Text="Click" FontSize="36"></TextBlock>
                        </StackPanel>
                    </ListViewItem>
                </ListView.Items>
            </ListView>
        </SplitView.Pane>

        <SplitView.Content>
            <Frame Name="MyFrame"></Frame>
        </SplitView.Content>
    </SplitView>
</Grid>

In code behind: 在后面的代码中:

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (MyListView.SelectedItem.Equals(FristItem))
    {
    }
    else if (MyListView.SelectedItem.Equals(SecondItem))
    {
    }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    mySplitView.IsPaneOpen = !mySplitView.IsPaneOpen;
}

Well there are some xaml examples of this available on GitHub... 好吧,GitHub上有一些xaml示例...

Here is one: https://github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml#L25 这是一个: https : //github.com/AppCreativity/Kliva/blob/master/src/Kliva/Controls/SidePaneControl.xaml#L25

And here is another one: https://github.com/JustinXinLiu/SwipeableSplitView/blob/master/GestureDemo/Shell.xaml#L103 这是另一个: https : //github.com/JustinXinLiu/SwipeableSplitView/blob/master/GestureDemo/Shell.xaml#L103

In short, you just add the ListView in your Pane part of the SplitView and take note of the DataTemplates used to be sure that you have an icon and text. 简而言之,您只需将ListView添加到SplitView的Pane部分中,并注意用于确保具有图标和文本的DataTemplates。

Like this: https://github.com/AppCreativity/Kliva/blob/c36d65058c4c35f0a3d2c7c886df81ba5ecfb31b/src/Kliva/XAMLResources/DataTemplates.xaml#L410 像这样: https : //github.com/AppCreativity/Kliva/blob/c36d65058c4c35f0a3d2c7c886df81ba5ecfb31b/src/Kliva/XAMLResources/DataTemplates.xaml#L410

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

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