簡體   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