WPF CollectionView Filter on Property of Model?
in a MVVM WPF app, how would I go about filtering a ListBox that is bound to a property of a model?
I want to filter out all the Y's that don't have a single X that matches the SearchText, I achieve this by setting the Filter property on YCollectionView in the ViewModel.
The problem is that even though that works, if you select a Y in the listbox it'll show every X, even if they don't match the text.
I can't set the Filter property on a CollectionView because ListOfX is a property of the Y model and I don't think I should use a CollectionView in the model, how would you normally achieve this behaviour?
is creating a ObservableListOfX and updating it in the setter of SelectedY a valid solution?
10 Replies
@Klarth so you are telling me to introduce a new observable list for the ListOfX from whatever Y that is selected in the viewmodel right? My question still remains as to where I should set that observablelist
you told me it shouldn't happen in the SelectedY setter but I wouldn't know where else to put it, besides in some SelectionChanged event from the listbox
sorry for tagging you here but you seem to be at least somewhat engaged with my question haha :p, I posted a more complete code mockup above
I'm trying hard to find a proper solution, because this problem is actually nested one level more, as in:
X has a list of Z, and I want to filter based on the name of Z and it should not show any Y's NOR X's that don't have a single X that don't have a single Z
Y
is the classic "if a tree falls in the forest and nobody is around, does it make a sound?". There are no notifications, no domain events, etc to announce changes.you mean ObservableListOfY? yeah I haven't implemented propertychange on there because that collection never changes
I could but it would be unused so I left it out of the code mockup
No, this part. Does it ever change?
no
but that's the model right? I don't think it should notify anything even if it did
If they're immutable, then just map them (ie. copy) to a new ViewModel type with an observable collection, use
CollectionViewSource
, and move on.
If you have an in-memory domain, you're either: 1. notifying of changes (not INPC, but events/messages) or 2. leaking logic of when and what to synchronize to the ViewModel.
Otherwise, you call a service to do some domain work...and you have no idea what happens if you don't do one of those two steps.yeah you're right on that actually, but it just gets read in at the start and it will never change
Then what's the big deal about mapping it to something View-friendly?
🤷♂️
so this is just mirroring the model but in the viewmodel space? I have never seen that before
Like I said elsewhere, the VM state is just a temporary, editable copy of Model(s) state. You have to copy because you aren't binding to directly to the Model.
If you wrap and automatically sync VM and Model state...then you're going to run into issues.