Reputation: 6518
In my scenario I have lots of views (UserControls) that as part of it contain some filter options. Lets for example say that there are in total 10 filter options, but not all views show all 10 filter options. Some of them show only 3, some of them 8, etc. The important thing is that when one filter option is set on one view, all views that contain this filter option should have the same value.
AView: filterOption1, FilterOption2, FilterOption3
BView: filterOption1, FilterOption3, FilterOption5
CView: filterOption2, FilterOption4, FilterOption6, FilterOption7
When user sets on AView filterOption1 = True, the same value should be set on all views that use filterOption1 (ex BView). This led me to decision to have one CommonViewModel (singleton), that will contain common (shared) data, and that will expose all filter options in it, and that all views should bind to this CommonViewModel.
In order to display filter options for particular view, I can think of two approaches:
1) design ONE CommonView, create new instance for each representation, and hide the filter options where not needed
<DataTemplate DataType="vm:CommonViewModel">
<vw:CommonView ShowFilterOption1="True" ShowFilterOption3="True" />
</DataTemplate>
2) Design separate view for each different representation (different filter options)
What do you think about these approaches, which one would you prefer, do you have some other ideas, and what would be the best choice to follow proper MVVM design?
Upvotes: 1
Views: 722
Reputation: 1345
To me, this kind of logic - whether to include a particular filterOption into a view or not, actually belongs to the view model, and not view. I would consider implementing one view, and separate view model instances for each of A,B,C. The view model can be one class, which is parametrized by filterOptions it will show.
Upvotes: 0
Reputation: 672
n views that bind to one viewmodel and once a property in viewmodel changes the views should update = binding twoways to that property
1) one view with all filter options means you will need custom logic for displaying only the filter options you wish to see. = binding to visibility
2) separate view for each different representation of filter option is also a possibility but i would do this only if futhermore each separate view contains some extra controls or logic which makes each separate view somehow unique else use 1).
Upvotes: 1