简体   繁体   English

更新分组的列表视图

[英]Updating grouped listview

I am working on an app which is displaying alot of items in ListViews and GridViews and i am grouping those items and using SemanticZoom control to make navigation easier. 我正在开发一个在ListViews和GridViews中显示大量项目的应用程序,并且正在将这些项目分组并使用SemanticZoom控件使导航更容易。 The items represent songs. 这些项目代表歌曲。 My problem is when the user deletes on of the items in a list (by holding down on the item and then selecting delete from the context menu) the item is deleted from the database but not from the listview or gridview so i have to listen to the CollectionChanged event and then re-load the grouping and assign it to the CollectionViewSource again which doesn't feel right. 我的问题是,当用户删除列表中的项目时(按住该项目,然后从上下文菜单中选择“删除”),该项目即从数据库中删除,而不是从listview或gridview中删除,因此我必须听然后单击CollectionChanged事件,然后重新加载分组,然后再次将其分配给CollectionViewSource,这感觉不对。

I would like to know if there is a way to achieve this effect in WinRT and how. 我想知道在WinRT中是否有一种方法可以达到这种效果,以及如何实现。 Mainly i want the interaction to be just like the WP contacts app, when the user deletes a contact it gets removed without the app re-loading the list. 主要是我希望交互就像WP联系人应用程序一样,当用户删除联系人时,无需应用程序重新加载列表即可将其删除。

Thank you. 谢谢。

I have been learning a lot about SemanticZoom the CollectionViewSource, and most important how to keep updating (usually forgotten in examples) the view without making weird things: 我一直在学习有关SemanticZoom的CollectionViewSource的很多知识,最重要的是如何在不做任何奇怪的事情的情况下保持视图的更新(通常在示例中被遗忘):

1.- Create a class in the Model that is like a view called GroupedSongs with a category class property (string, class, enum) and 1.-在模型中创建一个类,该类类似于名为GroupedSongs的视图,具有类别类属性(字符串,类,枚举),并且

2.- An ObservableCollection< Song> property with INotifyPropertyChanged 2.-具有INotifyPropertyChanged的ObservableCollection <Song>属性

3.- Create an ObservableCollection in your ViewModel called Groups for instance. 3.-在您的ViewModel中创建一个名为Groups的ObservableCollection。 (Never reset always clear after first instance) (永远不会复位总是在第一次发生后清除)

4.- Read the contents and fill the Groups ObservableCollection 4.-阅读内容并填写Groups ObservableCollection

5.- In XAML add the following resource: 5.-在XAML中添加以下资源:

<CollectionViewSource x:Name="CVS" ItemsPath="Songs"  IsSourceGrouped="True" Source="{Binding Groups}"/>

6.- In the Semantic Zoom In View: 6.-在语义放大视图中:

<ListView x:Name="ZoomedInView" ItemsSource="{Binding Source={StaticResource CVS}}"

7.- In the Semantic Zoom Out View 7.-在语义缩小视图中

<GridView  ItemsSource="{Binding Groups}"

That's the best option I achieved and if you add or delete songs from Groups you will see the UI refreshed. 这是我实现的最佳选择,如果您从网上论坛中添加或删除歌曲,您将看到UI刷新。

I have tested in Windows 10 for the app I am preparing 我已经在Windows 10中测试了正在准备的应用程序

So I solved the issue, by creating an ObservableCollection of ObservableCollections like: ObservableCollection<ObservableCollection<ItemClass>> and when I update the items (adding or removing an item) I just find the inner collection that has the item and make the changes. 因此,我通过创建一个ObservableCollections的ObservableCollection来解决了这个问题,例如: ObservableCollection<ObservableCollection<ItemClass>>并且当我更新项目(添加或删除项目)时,我只是找到了具有该项目的内部集合并进行了更改。 The UI will reflect the changes nicely. 用户界面将很好地反映更改。

Edit: Just to clarify, To enable the listview or gridview to display headers I've inherited from ObservableCollection to create this class: 编辑:只是为了澄清,要使listview或gridview能够显示我从ObservableCollection继承的标题来创建此类:

public class KeyedList<T> : ObservableCollection<T>
{
    public object Key
    { get; set; }

    public KeyedList()
    { }

    public KeyedList(IGrouping<object, T> group)
    {
        Key = group.Key;
        foreach (var member in group)
            Add(member);
    }
}

When I initialize the class, T will be of type ObservableCollection: 当我初始化类时,T将是ObservableCollection类型:

KeyedList<ObservableCollection<Song>> MySongs = new .....

Hope this will help someone. 希望这会帮助某人。

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

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