INotifyCollectionChanged [英] INotifyCollectionChanged

查看:39
本文介绍了INotifyCollectionChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将GridView绑定到CollectionViewSource,因此:


        < GridView中的ItemsSource = QUOT; {绑定源= {StaticResource的ItemsViewSource}}"


在CVS被这样定义:


&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < CollectionViewSource X:名称= QUOT; ItemsViewSource"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;源= QUOT; {结合基团}"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; IsSourceGrouped = QUOT;真"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ItemsPath = QUOT;物品" />


组是从的ObservableCollection衍生所以实现INotifyCollectionChanged&NBSP;


这个问题似乎是,即使物品(ItemsPath上文)实现INotifyCollectionChanged,当我从Items添加,更改或删除项目时,网格视图不会更新。我必须强制重新绑定CVS。

解决方案

也许这会有所帮助:


- 生成一个新的网格应用程序

- 在GroupedItemPage上添加一个Button,并在下面添加一个代码隐藏代码段。
- 运行并单击按钮:组1已更新


<预类= "prettyprint">私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
变种GRP = SampleDataSource.GetGroup(QUOT;组-1。");
grp.Items.Insert(0,new SampleDataItem(" Group-1-Item-1",
" Item Title:1",
" Item Subtitle:a new Item" ;,
"Assets / LightGray.png",
"A new Item",
"A new Item",
grp));
}


通过在调试步骤模式中执行上述"插入",您可能会发现"TopItems"与"TopItems"之间的区别。组更新在Grid App模板和您的实现中处理。


I'm binding a GridView to a CollectionViewSource, thus:

        <GridView ItemsSource="{Binding Source={StaticResource ItemsViewSource}}"

The CVS is defined thus:

        <CollectionViewSource x:Name="ItemsViewSource"
                              Source="{Binding Groups}"
                              IsSourceGrouped="true"
                              ItemsPath="Items" />

Groups is derived from ObservableCollection so implements INotifyCollectionChanged. 

The problem seems to be that even though the Items (ItemsPath above) implements INotifyCollectionChanged, when i add, change or remove items from Items, the grid view doesnt update. I have to force a re-bind of the CVS.

解决方案

Maybe this will help:

- generate a new Grid App
- add a Button on the GroupedItemPage and the code-behind snippet below
- run and click Button: Group 1 is updated

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var grp = SampleDataSource.GetGroup("Group-1");
            grp.Items.Insert(0, new SampleDataItem("Group-1-Item-1",
                                             "Item Title: 1",
                                             "Item Subtitle: A new Item",
                                             "Assets/LightGray.png",
                                             "A new Item",
                                             "A new Item",
                                             grp));
        }

By following the above 'Insert' in debug step mode, you'll probably find the difference between how the "TopItems" group update is handled in the Grid App template and your implementation.


这篇关于INotifyCollectionChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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