繁体   English   中英

如何在UWP应用中检测何时将项目添加到ListView或从ListView中删除?

[英]How to detect when a item is added to or removed from a ListView in UWP app?

似乎没有事件可以检测何时向ListView中添加项目或从中删除项目……非常奇怪,当它似乎是开发人员通常会使用的东西时……我是否缺少某些东西?

如果ListView的ItemSource绑定到ObservableCollection,则在将项目添加或删除到集合中时,您可以轻松获得通知(这也会导致ListView发生相同的更改)。

XAML

<ListView ItemsSource="{x:Bind MyCollection}"/>

后端

public ObservableCollection<Person> people {get;set;} = new ObservableCollection<Person>();

然后您可以订阅此集合的OnCollectionChanged事件,例如:

people.OnCollectionChanged += (s,e) => {//you logic goes here}

有关如何将ObservableCollection绑定到ListView的更多信息: https : //social.technet.microsoft.com/wiki/contents/articles/32820.uwp-binding-observeable-collection-to-listview-control.aspx

暂无
暂无

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

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