C#MVVM TreeView双向数据绑定 [英] C# MVVM TreeView TwoWay-Binding of hierarchical data

查看:405
本文介绍了C#MVVM TreeView双向数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌搜索了两个多星期的答案.这通常意味着我要么是盲目的,要么是荒谬的.无论如何:

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. 音频(类)
    • BaseDir(结构)
    • PlayMode(枚举)
    • 输入(类)
    • CalibrateOnConnect(布尔)
    • KnownDevices(集合)
  1. Audio (class)
    • BaseDir (struct)
    • PlayMode (enum)
    • Input (class)
    • CalibrateOnConnect (bool)
    • KnownDevices (collection)
  1. ...(类)
    • ...
  1. ... (class)
    • ...

  • UseDevice(整数)
  • 播放列表(收藏夹)

  • UseDevice (integer)
  • Playlist (collection)

    1. FirstAudio(类)
      • 路径(字符串)
      • 重复(整数)
    1. FirstAudio (class)
      • Path (string)
      • Repeat (integer)

  • ...
  • 我已经设法以MVVM模式在TreeView中显示这些内容.由于我无法确切确定将来会添加哪些选项,因此我使用了一种通用方法,为classienumerable,我的自定义structs和基本值类型(stringboolenum,...).

    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, ...).

    在我的XAML中,它们具有对应的(分层)DataTemplate,例如带有用于布尔值的CheckBox或用于常规值类型的文本块.

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

    每个ViewModel实例都有一个字段来存储基础Model的数据.

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

    我设法在View中以及通过ViewModel中的TwoWay-Binding编辑这些值.
    但是令我头疼的是如何在Model中更新此数据.
    由于我通过反射创建了分层的ViewModel结构,因此父级没有对应于等效配置类/集合的字段名称/索引器/...的获取/设置属性.尽管每个ViewModel实例都知道创建它的父模型的数据结构的字段/属性名称,并且也知道其父ViewModel实例(但不知道其父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.

    没有通过常规绑定技术实现此目的的简单方法吗?
    如果为我使用的每个配置(子)类创建ViewModels会更好吗?
    提示:每个ViewModel实例在父级模型的数据结构中都知道其字段/属性名称.

    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.

    推荐答案

    通读注释,我认为视图模型应该与它要表示的模型紧密耦合.拥有动态视图模型也很难测试.

    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.

    但是,如果您真的想尝试解决此问题,则可以考虑使用 DynamicObject ,它使您能够以完全通用的方式获取/设置实例的属性.使用DynamicObjects,您将获得在设置或获取属性时调用的挂钩,例如,这将使您(例如)引发给定动态属性的NotifyPropertyChange事件.

    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.

    这篇关于C#MVVM TreeView双向数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆