C# 从 WPF 绑定列表框中删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19937927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing an item from a WPF binding listbox
提问by mmvsbg
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:
我有一个带有 ListBox(称为 listMyItems)的 WPF 应用程序,它成功绑定到我创建的 MyItems 类。我有一个名为 currentMyItems 的 MyItems 列表,然后将其作为 ItemSource 分配给 ListBox。一切正常,如果我向 currentMyItems 添加一个项目,它会在列表中弹出,等等。当我尝试删除 ListBox 中的选定项目时会出现问题。这是我使用的代码:
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?
该项目从 ListBox 中消失,但下次我更新它时,它会重新弹出,因为它从未被删除。有小费吗?
采纳答案by Rachel
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.
我想您可能对数据绑定的工作方式感到困惑。当您绑定一个属性时,您是在告诉 WPF 去其他地方寻找该属性的值。
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.
当您将ListBox.ItemsSource
属性绑定到 时currentMyItems
,您是在告诉 WPF 查看currentMyItems
列表以查找其项目列表。如果currentMyItems
是 aObservableCollection
而不是 a List<T>
,则当您从集合中添加或删除项目时,UI 将自动收到更新绑定值的通知。
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.
根据您在问题中所说的,听起来您有两个集合,其中一个是绑定的,另一个用于在发生更改时重新创建第一个集合。所有不需要的。
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.
只需创建一个ObservableCollection<MyItem>
,将其绑定到ListBox.ItemsSource
属性,然后从该单个集合中添加或删除项目。它应该像您期望的那样工作。
<ListBox x:Name="listMyItems" ItemsSource="{Binding MyItems}" />
and
和
MyItems.Add((MyItem)listMyItems.SelectedItem)
MyItems.Remove((MyItem)listMyItems.SelectedItem)
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 WPFand What is this “DataContext” you speak of?
如果您有兴趣,我的博客上还有一些初学者文章,供正在努力理解 DataContext 的 WPF 用户使用。您可能想查看了解从 WinForms 切换到 WPF 时思维方式的变化以及您所说的“DataContext”是什么?
回答by Andrzej Gis
If you bound it correctly to an ObservableCollection and currentMyItems is that collection. Than it means that you must have reloaded currentMyItems in meantime.
如果您将其正确绑定到 ObservableCollection 并且 currentMyItems 就是该集合。这意味着您必须同时重新加载 currentMyItems 。
Also consider binding the SelectedItem property of your ListView - your view model doesn't have to know about the view at all.
还要考虑绑定 ListView 的 SelectedItem 属性 - 您的视图模型根本不必了解视图。
回答by C_sharp
Make the currentMyItems<MyItem>
an ObservableColection<MyItem>
. This way it will raise a property change whenever modified and the UI gets updated accordingly.
做出currentMyItems<MyItem>
的 ObservableColection<MyItem>
。这样,它会在每次修改时引发属性更改,并且 UI 会相应地更新。
回答by Kristian Nielsen
By using ObservableCollection you will automatically get updates on the UI.
通过使用 ObservableCollection,您将自动获取 UI 上的更新。
You should use an ObservableCollection instead of List. A good thing is to always use ObservableCollection instead of List when something to do with UI
您应该使用 ObservableCollection 而不是 List。一件好事是在处理 UI 时总是使用 ObservableCollection 而不是 List
回答by Smagin Alexey
Your source collection must be modufy (inherit from IList or ICollection). If your source collection does not support this method of your interface Remove, you can't remove item from source.
您的源集合必须是可修改的(从 IList 或 ICollection 继承)。如果您的源集合不支持您的接口 Remove 的此方法,则无法从源中删除项目。
So, when you want to remove item you must cast ItemsSource to IList or ICollection:
因此,当您想要删除项目时,您必须将 ItemsSource 转换为 IList 或 ICollection:
var source = listbox.ItemsSource as IList ?? listbox.ItemsSource as ICollection;
and then check:
然后检查:
if (source == null) return;
then:
然后:
listbox.SelectedItems.ForEach(source.Remove);
listbox.Items.Refresh();