【问题标题】:How to bind the view to the VM wrapper instead of directly to the Model when multiple levels多级时如何将视图绑定到 VM 包装器而不是直接绑定到模型
【发布时间】:2015-01-25 21:45:09
【问题描述】:

假设我有一个这样的模型结构

Being
|_ Person
   |_ Billing Address
   |_ Customer
      |_ Shipping Addresses (Collection)
      |_ etc..
   |_ etc..

我的观点是这样的

  ListBox (ItemsSource - bound to a VM wrapper of "Being")
    - DataTemplate containing ListBox2 (ItemsSource - bound to a VM wrapper of "Person")
       - DataTemplate containing ListBox3 (ItemsSource that I want to be bound to "Shipping Adresses") PROBLEM!

我正在使用 MVVM Light 和定位器,但无法弄清楚如何执行第三级 - 第三个第二个 DataTemplate (ListBox3) 中的“Shipping Adresses”绑定。它只允许我直接绑定到模型中的送货地址集合。但我想在客户的 VM 包装器中执行此操作,因为我需要对集​​合做一些事情。任何代码都将有助于内部绑定。

【问题讨论】:

    标签: c# xaml mvvm binding mvvm-light


    【解决方案1】:

    如果我了解您的模型层次结构,看起来送货地址是第 4 级(除非您的意思是“存在”是抽象的或零)。您应该能够像这样深入了解每个模型的成员

    <Listbox ItemsSource="{Binding Being}">
        <DataTemplate>
            <Listbox ItemSource="{Binding Person}">
                <DataTemplate>
                    <Listbox ItemSource="{Binding Customer}">
                        <DataTemplate>
                            <Listbox ItemSource="{Binding ShippingAddress}"/>              
                        </DataTemplate>
                    </Listbox>                
                </DataTemplate>
            </Listbox>
        </DataTemplate>
    </Listbox>
    

    此外,我相信点符号在 XAML 中适用于属性。所以尝试这样的事情

    <Listbox ItemsSource="{Binding Being}">
        <DataTemplate>
            <Listbox ItemSource="{Binding Person}">
                <DataTemplate>
                    <Listbox ItemSource="{Binding Customer.ShippingAddresses}"/>
                </DataTemplate>
            </Listbox>
        </DataTemplate>
    </Listbox>
    

    如果你能发布一些代码sn-ps,我相信可以找到解决方案!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多