从WPF绑定列表框中删除的项 [英] Removing an item from a WPF binding listbox

查看:946
本文介绍了从WPF绑定列表框中删除的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有哪些成功绑定到一个类,我创建MyItems的列表框(称为listMyItems)一个WPF应用程序。我有MyItems叫currentMyItems然后将其分配为的ItemSource到ListBox的列表。这一切工作正常,如果我添加一个项目到currentMyItems它列表等上弹出
当我尝试删除ListBox中选定的项目出现问题。这是code,我使用:

I have a WPF application with a ListBox (called listMyItems) which is successfully bound to a class of MyItems that I created. I have a List of MyItems called currentMyItems which is then assigned as ItemSource to the ListBox. It all works fine, if I add an item to the currentMyItems it pops up on the list, etc. The problem occurs when I try to remove the selected item in the ListBox. This is the code that I use:

currentMyItems.Remove((MyItem)listMyItems.SelectedItem);

该项目从列表框消失,但下一次我更新它,它会弹出备份,因为它从来没有被删除。任何提示?

The item disappears from the ListBox but the next time I update it, it pops back up as it was never deleted. Any tips?

推荐答案

我想你可能会感到困惑数据的方式结合的作品。当您绑定的属性,你告诉WPF中去看看别的地方该属性的值。

I think you may be confused about how data binding works. When you bind a property, you are telling WPF to go look somewhere else for the value of that property.

当您绑定 ListBox.ItemsSource 属性 currentMyItems ,你告诉WPF中去看看 currentMyItems 列表中找到它的产品清单。如果 currentMyItems 的ObservableCollection ,而不是列表< T> ,那么UI就会自动当你从集合中添加或删除项目收到通知更新绑定值。

When you bind the ListBox.ItemsSource property to currentMyItems, you are telling WPF to go look at the currentMyItems list to find its list of items. If currentMyItems is an ObservableCollection instead of a List<T>, then the UI will automatically receive a notification to update the bound value when you add or remove an item from the collection.

根据您的问题说什么,它听起来就像你有两个集合,其中一个是绑定的,它是用来重新创建第一个集合随时发生变化另一方。所有这一切都没有必要

Based on what you say in the question, it sounds like you you have two collections, one of which is bound, and the other which is used to recreate the first collection anytime a change occurs. All that is not needed.

只需创建一个的ObservableCollection&LT; MyItem&GT; ,其绑定到 ListBox.ItemsSource 属性,然后添加或从中删除单个集合项目​​。它应该如你所愿。

Just create one ObservableCollection<MyItem>, bind it to the ListBox.ItemsSource property, and then add or remove items from that single collection. It should work as you would expect.

<ListBox x:Name="listMyItems" ItemsSource="{Binding MyItems}" />

MyItems.Add((MyItem)listMyItems.SelectedItem)
MyItems.Remove((MyItem)listMyItems.SelectedItem)

如果你有兴趣,我也有我的博客上谁正在努力理解DataContext的WPF用户的一些初学者的文章。你可能想看看<一个href=\"http://rachel53461.word$p$pss.com/2012/10/12/switching-from-winforms-to-wpfmvvm/\">Understanding从的WinForms WPF的并的这是什么的DataContext你说的?

If you're interested, I also have some beginner articles on my blog for WPF users who are struggling to understand the DataContext. You may want to check out Understanding the change in mindset when switching from WinForms to WPF and What is this "DataContext" you speak of?

这篇关于从WPF绑定列表框中删除的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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