I am developing a data visualisation tool, with some UI interactivity after initial trail with doing it mostly by using templates in XAML and code behind to handle interactive stuff I've realised that too much UI specific information leas into View Model and realised that I would be better off with Custom Controls.
Currently I came up with a sketch of an architecture which comprises of:
- Custom
ListBox
to visualise data in some custom way using control template to specify visuals and code to specify validation logic, custom behavior of theListBox
. - a class deriving from
ListBoxItem
with additional properties which help to visualise the data and custom template for that. - Custom
Panel
which has all the zoom, and moving stuff around, which is thenItemsHost
for theListBox
My main issues is that:
I need to develop two controls, which has a lot of overlap with the custom ListBox
control I have developed. But they need to have some different options and the underlying business logic to calculate the visual representation of the data is different. I think the best approach is to have them as UserControl
s which would use the main custom ListBox
and they will have their own View Models which would have all the commands and would expose Business logic model. It seems the easiest is to create a DependencyProperty
which will be of a View Model type.
In this way I think I can have nice separation of concerns:
- Custom Control which is part of the View it has logic related to View, which is encapsulated within its class. This is the custom
ListBox
and with its associatedPanel
and customListBoxItem
and all the custom classes responsible for the View logic. - User Controls which I interpret as encapsulated part of the View, where you just assemble existing controls. They use the custom
ListBox
developed previously. View is dumb here really and all the calculation is done in the Model exposed by the View Model. - View Models for the controls for the commands.
- Main View Model handles higher level logic and the objects for the Sub View Models are created in its constructor
- Main View defines overall application View and the bindings including binds from sub View Models to User Controls
I am not sure if creating a dependency property of the type of a specific sub View Model on a user control and then binding it is the right approach, as usually the binding is done by setting DataContext
in the XAML, but that does not work for me as I have issues with having two data contexts. Currently my approach seems fine, but I don't know whether I will encounter some problems in the future.