UWP ListView:选择时如何展开项目? [英] UWP ListView: How to expand an item when select it?

查看:27
本文介绍了UWP ListView:选择时如何展开项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些项目的列表视图.我想在选择一项时展开该项以显示详细信息,我该怎么办?

I have a listview containing some item. And I want to expand the item to show detail information when I select one item, what should I do?

推荐答案

我没有仔细检查@AVK Naidu 提供的 CustomControl ,它很好,似乎可以解决您的问题.但我需要在这里说,使用默认的 ListView 控件完全有可能完成这项工作,您需要的只是更改 ListViewItem<的 DataTemplate/code> 当它被选中时.

I didn't check the CustomControl carefully which provided by @AVK Naidu, which is good and seems can solve your problem. But I need to say here, it is totally possible to do this work with the default ListView control, what you need is just changing the DataTemplate for your ListViewItem when it is selected.

仅以此处为例:

<Page.Resources>
    <DataTemplate x:Name="Normal" x:Key="Normal">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
    <DataTemplate x:Name="Detail" x:Key="Detail">
        <StackPanel>
            <TextBlock Text="{Binding Name}" FontSize="30" Foreground="Red" HorizontalAlignment="Center" />
            <TextBlock Text="Details:" FontSize="30" Foreground="Blue" Margin="0,10" />
            <TextBlock Text="{Binding Details}" FontSize="20" />
        </StackPanel>
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemTemplate="{StaticResource Normal}"
              ItemsSource="{x:Bind Collection}" SelectionChanged="listView_SelectionChanged" />
</Grid>

listView_SelectionChanged 的代码:

private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Assign DataTemplate for selected items
    foreach (var item in e.AddedItems)
    {
        ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
        lvi.ContentTemplate = (DataTemplate)this.Resources["Detail"];
    }
    //Remove DataTemplate for unselected items
    foreach (var item in e.RemovedItems)
    {
        ListViewItem lvi = (sender as ListView).ContainerFromItem(item) as ListViewItem;
        lvi.ContentTemplate = (DataTemplate)this.Resources["Normal"];
    }
}

结果:

这篇关于UWP ListView:选择时如何展开项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆