views:

206

answers:

1

Should you use CollectionViewSource in the WPF Custom Control's code behind?

I'am asking because the CollectionViewSource.GetDefaultView(SOURCE) with .Filter set changes the view so that all instances of the Control have the same view.

Doesn't it mean, that in the Custom Control's code behind, you should avoid use of CollectionViewSource ?

+1  A: 

It depends on the desired behavior. E.g. if you had multiple lists of states on the screen and wanted to filter all of them by region at the same time, then this could be desired behavior. You can always force a different view source in cases where you do not want the items bound to the same filter criteria.

I think avoiding the use of CollectionViewSource until you understand the interactions of ItemsSource -> CollectionView -> Bound Item controls is probably a good idea, but I don't know that I would go so far as to say that you should avoid it in general.

I think it might be reasonable to say that if two lists on a form have different filtering/sorting requirements then they should be bound from different source properties (even if those properties are straight clones of a single underlying source). That way you can still apply filtering and storing at the CollectionViewSource level but without unintended consequences later on.

Ben Von Handorf
You perfectly understood my point, and that's the big question: If MyControl users will tolerate that every control has its own DataSource although these sources are an identical clone. Let's think of persons list that has to be Nx added to the DynamicSource. This really solves the problem, but the question is: Is it acceptable in your opinion ?
PaN1C_Showt1Me
If you're developing an enclosed control for others I suspect you would want to hide that complexity from them. I would probably bind to a custom `CollectionViewSource` yourself in your own bindings.To keep your own code-behind complexity down, you could define `CollectionViewSource`s in your control's resource section, all bound to the same external source, and then bind your individual item controls to the appropriate `CollectionViewSource`.
Ben Von Handorf