MVVM WPF创建子元素

时间:2009-07-10 15:30:31

标签: wpf mvvm

我有一个Customer对象,其中包含一个Orders列表。现在,使用MVVM模式我正在显示客户列表,它是CustomerOrderViewModel和“CustomerOrderView”的一部分。使用下面实现的列表框显示客户:

  <ListBox BorderThickness="0" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path=Customers}">                
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                        <view:CustomerView />                
                       </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>                
            </ListBox>

现在我还需要显示订单,但我需要在ListBox外面显示它。像这样:

 <StackPanel Grid.Column="1" Grid.Row="0" Margin="10">

                <ItemsControl ItemsSource="{Binding Path=Orders}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

            </StackPanel>   

这不起作用,因为订单的CustomerOrderViewModel上没有属性。 Orders是Customer对象的集合。我怎样才能实现它?

以下是更新的示例:

<ListBox BorderThickness="0" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path=Customers}">                
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                        <view:CustomerView />

                            <StackPanel Margin="20">

                                <ItemsControl ItemsSource="{Binding Path=Orders}">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <view:OrderView />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

                            </StackPanel>

                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>                
            </ListBox>

我不想显示所有客户的订单。我只想显示当前所选客户的订单。

2 个答案:

答案 0 :(得分:1)

您可以使用master-detail绑定。

答案 1 :(得分:0)

我建议您在窗口中添加一个额外的List,并将其DataContext绑定到ListBox中当前选定的客户。它会是这样的:

        <ListBox BorderThickness="0" Grid.Column="0" Grid.Row="0"
            ItemsSource="{Binding Path=Customers}"
            x:Name="CustomerList">
            <ListBox.ItemTemplate>  
                <DataTemplate>  
                    <view:CustomerView />
                </DataTemplate>  
            </ListBox.ItemTemplate>                  
        </ListBox> 

        <ListBox Grid.Column="1" Grid.Row="0" DataContext="{Binding ElementName=CustomersList, Path=SelectedItem}">
           <ListBox.ItemTemplate>
               <DataTemplate>
                   <view:Order DataContext="{Binding Path=Orders}" />
               </DataTemplate>
           </ListBox.ItemTemplate>
        </ListBox>