C# MVVM TreeView TwoWay-Binding of hierarchical data

落花浮王杯 提交于 2019-12-13 03:35:27

问题


I googled for an answer to this for more than two weeks now. This usually means either I am blind or the idea is absurd. Anyways:

In a middle-sized, quite flexible project I'm storing configuration data in a hierarchical structure in the like of this one:

  • Configuration (collection)
    1. Audio (class)
      • BaseDir (struct)
      • PlayMode (enum)
      • Input (class)
      • CalibrateOnConnect (bool)
      • KnownDevices (collection)
        1. ... (class)
          • ...
      • UseDevice (integer)
      • Playlist (collection)
        1. FirstAudio (class)
          • Path (string)
          • Repeat (integer)
      • ...

I already managed to display these in a TreeView in a MVVM pattern. Since I can't exactly tell which options will be added in future, I used a generic approach, creating ViewModels for class, ienumerable, my custom structs and the basic value types (string, bool, enum, ...).

In my XAML these have their corresponding (Hierarchical)DataTemplates, e.g. with a CheckBox for booleans or a textblock for general value types.

Each ViewModel instance has a field to store the data of the underlying Model.

I manage to edit these values in the View and via TwoWay-Binding also in the ViewModel.
But what makes my head hurt is how to update this data in the Model.
Since I create the hierarchical ViewModel structure through reflection, the parents do not have get/set properties corresponding to the field names / indexers / ... of the equivalent configuration class / collection. Though each ViewModel instance knows the field/property name of the parent's model's data structure it was created from and knows its parent ViewModel instance, too (but not its parent Model instance).
Every attempt to solve this problem via Commands or by calling some parent's update function made a knot into my brain.

Isn't there a simple way to achieve this via regular binding techniques?
Would it be better if I created ViewModels for each configuration (sub)class I'm using?
Hint: Each ViewModel instance knows its field / property name in the parent's model's data structure.


回答1:


Reading through the comments I'd argue that a view model should be tightly coupled to the model that it is trying to represent. Having a dynamic view model is also very hard to test.

However, if you really wanted to try to get around this, you could consider using a DynamicObject which allows you to get/set properties on the instance in a completely generic fashion. With DynamicObjects you are then given hooks that are called when a property is either set or got, this would enable you (for example) to raise NotifyPropertyChange events for a given dynamic property.



来源:https://stackoverflow.com/questions/3257673/c-sharp-mvvm-treeview-twoway-binding-of-hierarchical-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!