基于属性部分绑定到列表或 ObservableCollection

发布于 2024-12-05 23:17:04 字数 1095 浏览 3 评论 0原文

我一直在寻找,但我还没有找到遇到与我试图解决的相同问题的人 - 但我认为这是很多人可能从中受益的类型:

简单地说,我'我喜欢一个列表(理想情况下是一个 ObservableCollection),我可以将它绑定到 ListBox、DataGrid、ComboBox,等等。但我不仅仅想简单地绑定到整个列表 - 相反,我想绑定到列表中匹配条件的特定集合。我希望它是可观察的,以便该项目能够自行管理并且不会重置任何内容。另一种表达方式是过滤绑定,也许...

例如,想象一个列表:

FilteredObservableCollection<Person> people = new FilteredObservableCollection<Person>();
people.Add(new Person() { Name = "John Smith", IsMale = True });
people.Add(new Person() { Name = "Jane Doe", IsMale = False });
people.Add(new Person() { Name = "Fanny Mae", IsMale = False });
people.Add(new Person() { Name = "Freddie Mac", IsMale = True });

然后我将绑定我的 DataGrid:

myDataGrid.ItemsSource = people;

myDataGrid 默认情况下会列出所有人员。

但是如果我设置:

people.Filter = "IsMale"

FilteredObservableCollection 将只是一个男性列表(同时仍然保持完整且未过滤的列表)

或者也许我想太多了 - 也许通过转换器和触发器的一些棘手使用是可能的?我确实知道有一些控件 - Telerik 控件和其他控件 - 可以在数据网格上提供一定程度的过滤,但我正在寻找一种更多位于集合一侧的解决方案,而不是集合所针对的对象绑定。

不管怎样,在我派生我自己的 ObservableCollection 之前,我想我应该看看其他人经历过什么......

提前致谢!

I've looked and looked, but I have yet to find someone who's faced the same problem that I'm trying to solve - and yet I think it's the type of thing a lot of people could probably benefit from:

Simply put, I'd like a List (an ObservableCollection, ideally) that I could bind to a ListBox, a DataGrid, ComboBox, what have you. But I don't just want to simply bind to the entire list - rather, I'd like to bind to a particular set within the list that match a criteria. I'd like it to be Observable so that the item manages itself and nothing is reset. Another way to put it is Filtered Binding, perhaps...

For example, imagine a list:

FilteredObservableCollection<Person> people = new FilteredObservableCollection<Person>();
people.Add(new Person() { Name = "John Smith", IsMale = True });
people.Add(new Person() { Name = "Jane Doe", IsMale = False });
people.Add(new Person() { Name = "Fanny Mae", IsMale = False });
people.Add(new Person() { Name = "Freddie Mac", IsMale = True });

I'd then bind my DataGrid:

myDataGrid.ItemsSource = people;

The myDataGrid would list all people by default.

But then if I set:

people.Filter = "IsMale"

FilteredObservableCollection would only be a list of males (while still maintaining the complete, and un-filtered list)

Or maybe I'm over-thinking this - maybe it's possible with some tricky use of Converters and Triggers? I do know that there are some controls - Telerik controls, and others - which can provide some level of filtering on datagrids, but I'm looking for a solution that's more on the side of the Collection, rather than the object to which the collection binds.

Anyway, before I go deriving my own ObservableCollection, I figured I'd see what other people have experienced...

Thanks in advance!

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

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

发布评论

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

评论(1

猫七 2024-12-12 23:17:04

有一些 LINQ 风格的扩展方法的开源库可以实现此目的。

这个线程对它们进行了很好的比较...我们使用 BindableLinq 取得了巨大成功,尽管主要开发人员不再维护它。

There are a few open-source libraries of LINQ-styled extension methods that achieve this.

This thread makes a good comparison of them... We use BindableLinq to good success though the primary developer is no longer maintaining it.

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