如何在多个级别将视图绑定到VM包装器而不是直接绑定到Model

时间:2014-11-27 10:22:49

标签: c# xaml mvvm binding mvvm-light

说我有像这样的模型结构

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 Addresses Collection。但我想在Customer的VM包装器中执行此操作,因为我需要对集​​合执行某些操作。任何代码都有助于内部绑定。

1 个答案:

答案 0 :(得分:0)

如果我理解你的模型层次结构,它看起来像送货地址是第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>

如果您可以发布一些代码段,我相信可以找到解决方案!

相关问题