Partial Binding to a List or ObservableCollection, Based on a Property
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!
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.
精彩评论