Windows应用商店的应用程序:如何让ListView的具有可扩展/ enlargeable ListItems的? [英] Windows Store App: How to make ListView with expandable/enlargeable ListItems?

查看:255
本文介绍了Windows应用商店的应用程序:如何让ListView的具有可扩展/ enlargeable ListItems的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目列表视图,在C#中的Windows Store应用(是你叫这些是什么?我听说他们不叫地铁应用了)。

I have a Listview with items, in a C# Windows Store App (is that what you call these? I heard they're not called Metro Apps anymore).

ExpandableListView 在Android的相似,我希望能够为在listItems中的自来水在扩展列表项作为它崩溃了,如果你在另一个列表项挖掘,目前扩大(不是按钮),该列表项扩大,自来水列表项将崩溃,另一个将扩大。

Similar to the ExpandableListView in Android, I want to be able to tap on listitems (not the buttons) for that listitem to expand, tap on the expanded listitem for it to collapse, and if you tap on another listitem, the currently expanded listitem will collapse and the other will expand.

在我的具体情况我有一个的DataTemplate 为listItems中的两个扩展和非扩展视图。我已经看到了Android的ExpandableListView可以扩大与的其他的信息(的从WPF扩展做,而不是类似的东西),具有较大的项目取代它,但有一个常见的​​解决方案在Windows商店应用程序?
如果没有,什么是最接近的等效?

In my particular case I have a DataTemplate for both the expanded and non-expanded view of the listitems. I've seen that Android's ExpandableListView can expand the listitem with additional information (the Expander from WPF does something similar to that), instead of replacing it with a larger item, but is there a common solution for this in Windows Store Apps? If not, what is the closest equivalent?

喜欢就如下图,我想知道,如果有,可以扩大这种方式listItems中,如果没有,我有替代品的组成部分:

Like on the following drawing, I want to know if there is a component that can expand listitems in this way, or if not, which alternatives I have:

推荐答案

我结束了与该作品,但看起来并不过于花哨的解决方案。它切换的DataTemplate 当你点击的项目,但没有动画。它立即切换

I ended up with a solution that works but doesn't look too fancy. It switches DataTemplate when you click items but there's no animation: it switches instantly.

下面是最重要code部分:

Here's the important code parts:

<Page.Resources>
    <DataTemplate x:Key="dtSmall">
        <!--Component template for the un-expanded listitems-->
    </DataTemplate>
    <DataTemplate x:Key="dtEnlarged">
        <!--Component template for the expanded listitems-->
    </DataTemplate>
</Page.Resources>
<Grid>
    <ListView x:Name="lvEnlargeable"
        IsItemClickEnabled="True"
        ItemTemplate="{StaticResource dtSmall}"
        ItemsSource="{Binding ...}"
        SelectionChanged="LVEnlargeable_SelectionChanged"
        ItemClick="LVEnlargeable_ItemClick"/>
</Grid>

XAML.CS

public sealed partial class MainPage : Page
{
    private DataTemplate dtSmall;
    private DataTemplate dtEnlarged;

    public MainPage()
    {
        this.InitializeComponent();
        dtSmall = (DataTemplate)Resources["dtSmall"];
        dtEnlarged = (DataTemplate)Resources["dtEnlarged"];
    }

    // A selected item is treated as an expanded/enlarged item
    private void LVEnlargeable_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        /* First we set all the items that has been deselected
        to be collapsed, aka. using the dtSmall DataTemplate.
        We expect 0 or 1 item to have been deselected
        but handle all cases easily with a foreach loop.
        */
        foreach (var item in e.RemovedItems)
        {
            // Set the DataTemplate of the deselected ListViewItems
            ((ListViewItem)(sender as ListView).ContainerFromItem(item)).ContentTemplate = dtSmall;
        }

        /* Then we set all the items that has been selected
        to be expanded.
        We should probably throw an Exception if more than 1 was found,
        because it's unwanted behavior, but we'll ignore that for now.
        */
        foreach (var item in e.AddedItems)
        {
            ((ListViewItem)(sender as ListView).ContainerFromItem(e.AddedItems[0])).ContentTemplate = dtEnlarged;
        }
    }

    /* We need click events because SelectionChanged-events
    cannot detect clicks on an already selected item */
    private void LVEnlargeable_ItemClick(object sender, ItemClickEventArgs e)
    {
        ListView lv = (sender as ListView);

        /* Having set the IsItemClickEnabled property on the ListView to True
        we have to handle selection events manually.
        If nothing is selected when this click occurs, then select this item*/
        if (lv.SelectedItem == null)
        {
            lv.SelectedItem = e.ClickedItem;
        }
        else
        {
            // Clicking on an expanded/selected/enlarged item will deselect it
            if (lv.SelectedItem.Equals(e.ClickedItem))
            {
                lv.SelectedItem = null;
            }
            else
            {   /* If it's not a selected item, then select it
                    (and let SelectionChanged unselect the already selected item) */
                lv.SelectedItem = e.ClickedItem;
            }
        }
    }
}

如果这个与世隔绝的code是足够的,对自己,对这个解决方案,我没有测试过,但我希望它是,并且至少这个code包含的关键点。这晚,我只是想发布的东西为好奇头脑的人。如果显示不是为你工作,那么请留下有关问题的评论,我会确保添加缺少的部分。

I haven't tested if this isolated code is enough, on its own, for this solution, but I hope it is, and this code at least contain the key points. It's late and I just wanted to post something for the curious-minded people. If this shows not to work for you, then please leave a comment about the issue and I'll make sure to add the missing parts.

我也与ListViewItemStyleContainer的ListViewItem的presenter搞砸有更好的选择效应等,但我想这是最好的保持短。如果您发现这个有趣的为好,然后随意离开该评论过了,我会尝试包括它。

I also messed with the ListViewItemStyleContainer's ListViewItemPresenter to have better selection effects etc. but I figure it's best to keep it short. If you find this interesting as well, then feel free to leave a comment for that too, and I'll try include it.

这篇关于Windows应用商店的应用程序:如何让ListView的具有可扩展/ enlargeable ListItems的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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