Consider a ViewModel
that exposes a tree defined in the Model
, which is then data-bound to a TreeView
. The tree is rather large and the model is used directly because it is essentially read-only with regards to the view. Now, the TreeView
lives under a TabControl
in the VisualTree, so an issue at this point is that the IsExpanded
and IsSelected
properties aren't preserved when switching between tabs. One hesitates to add these boolean properties to each node in the Model
, as this should be extended in the ViewModel
as a matter of principle. The tree is composed of polymorphic nodes, so if we were to create a ViewModel
node type that derives from the tree node types and adds these properties, it seems this would result in some hairy code in the ViewModel
:
That is, if the tree has an abstract NodeBase
, and then derived types Node1
, Node1
, ... NodeN
(the Model
's Node
s). The ViewModel
then has to encapsulate these nodes, so when creating a ViewModelNode
, if it has a reference to Node
and also references to child ViewModelNode
's for each descendent ViewModelNode
that encapsulates each descendent Model
's Node all the way down the tree, maintaining these child references in the ViewModel
identically to how they are maintained in the Model
, along with a reference to the Model
. i.e. all references in the Model
nodes are replicated in the ViewModel
nodes, in order for each Model
node to be encapsulated by a ViewModel
node. The existence of redundant references such as this, even if handled in the ViewModelNode
's constructor, just smells bad.
What is the most accepted means to extend each node in a tree in this scenario, without wholesale replication of the references as stated above? (And to a lesser point, is the mere mention of using the model directly by the view an unforgivable crime, or is this forgiven due to the circumstances?)