如何从可观察集合中删除项目

发布于 2024-09-07 13:24:59 字数 912 浏览 2 评论 0原文

我对这本小说表示歉意,但我想尽可能地解释一下我迄今为止所做的一切。

在我当前的项目中,我有一个应用程序,它使用一个以 形式提供集合的服务。由于我在应用程序中使用这些数据的方式,我必须将这些数据转换为可观察的集合。这样做是为了在选择和移动数据时,将使用 INotifyPropertyChanged 和 INotifyCollectionChanged 刷新应用程序 UI 更新。

我现在遇到的挑战是我有一个列表框,它绑定到列表框中的可观察集合,我有一个数据模板,可以呈现集合的项目。此数据模板包含一个按钮,需要允许用户单击每个项目的按钮以将其从集合中删除。 其用例是一个列表框,用于存储从网格视图中选择的选定名称。一旦用户从网格视图中选择了名称,它们就会被存储(作为队列在可观察集合中)并在 UI 中的列表框控件中呈现,该控件显示所有选定的名称。我需要为用户提供按所选顺序删除这些名称的能力。

从我读到的内容来看,没有办法枚举/索引可观察的集合。对于这种情况,您应该使用列表或数组。但是,为了使项目在列表视图中刷新,它们需要位于可观察集合中。 从我读到的内容来看,当事件被触发时,我需要将可观察集合转换为数组,然后评估数组以确定索引,然后相应地删除记录?

我想我可能会偏离这一点,因为我似乎过度设计了这个问题?上面的场景似乎不正确,因为我感觉好像我在集合之间进行了大量的转换,只是为了删除一条记录? 当集合呈现为列表框中的项目控件时,有谁知道从集合中删除记录(以任何选择的顺序)的有效方法? 我已经成功地使用RemoveAt()删除了添加到集合中的最后一条记录,但是我在随机删除记录方面没有取得任何成功。

事后思考:此问题的部分原因可能与以下事实有关:我在数据模板(控制项)中插入了一个按钮,因此在事件触发之前实际上并未选择该项目。按钮事件? 很抱歉在这个问题上胡言乱语,但我已经花了几个小时思考这个问题并取得了一些小小的进展。任何提示或想法将不胜感激!

I appologize for the novel but I wanted to explain as much as I have done thus far.

Within my current project I have an application that consumes a service that provides a collection as a <List>. Due to how I am using this data in the application I have had to convert this data to an observable collection. This was done so that as the data was selected and moved about the application UI updates would be refreshed using INotifyPropertyChanged and INotifyCollectionChanged.

Where I am having a challenge now is I have a listbox that is bound to the observable collection within the listbox I have a datatemplate that renders out the items of the collection. This data template contains a button which needs to allow the user to click the button for each item to remove them from the collection.
The use case for this is a listbox that stores selected name as chosen from a gridview. Once the user has selected names from the gridview they are stored ( within the observable collection as a queue) and rendered out in the UI in a listbox control which shows all selected names. I need to provide the user with the ability to remove these names in any order selected.

From what I have been reading there is no means to enumerate / index an observable collection. For situations such as this you should use List or an Array. However in order for the items to refresh in the list view they need to be in an Observable Collection.
From what I have read it appears that when the event is triggered I need to convert the observable collection to an Array and then evaluate the array to determine the index and then remove the record accordingly?

I think I may be off base on this as it seems like I am over engineering this problem? The above scenario does not seem correct is because I fell as if I am doing a lot of converting to and from the collections to just remove a record?
Does anyone know of an efficient means to remove records from a collection ( in any order selected) when the collection is rendered out as an items control within a listbox?
I’ve been successful in removing the last record added to the collection using RemoveAt() however I have not had any success in randomly removing records.

Afterthought: Part of this issue could be related to the fact that I have a button inserted within the datatemplate (control item) and as a result the item is not actually being selected before the event is fired on the button event?
Sorry for the rambling on this but I have had my head in this for hours and made minor progress. Any tips or ideas would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2024-09-14 13:24:59

ObservableCollection继承自 Collection实现 IList,因此您当然可以索引和枚举它。它有一个 Remove 方法,该方法采用要删除的对象并删除集合中的第一个匹配项和 RemoveAt方法,它接受一个索引并删除该索引处的项目。

根据您的事后想法,听起来您有一个带有创建按钮的 ItemTemplate 的 WPF ListBox。 ListBox 会将每个实例化模板的 DataContext 设置为要绑定到的列表中的项目,因此您可以从 Button 上的 DataContext 属性或使用 Binding 获取对创建 Button 的项目的引用。

ObservableCollection<T> inherits from Collection<T> which implements IList<T>, so you can certainly index and enumerate it. It has a Remove method that takes the object to remove and removes the first occurrence in the collection and a RemoveAt method that takes an index and removes the item at that index.

Based on your afterthought, it sounds like you have a WPF ListBox with an ItemTemplate that creates a Button. ListBox will set the DataContext of each instantiated template to the item in the list being bound to, so you can get a reference to the item that created a Button from the DataContext property on the Button or by using a Binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文