MVVM WPF创建子元素

MVVM WPF创建子元素,wpf,mvvm,Wpf,Mvvm,我有一个客户对象,它有一个订单列表。现在,我使用MVVM模式显示一个客户列表,它是CustomerOrderView模型和“CustomerOrderView”的一部分。客户使用列表框显示,如下所示: <ListBox BorderThickness="0" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Path=Customers}"> <ListBox

我有一个客户对象,它有一个订单列表。现在,我使用MVVM模式显示一个客户列表,它是CustomerOrderView模型和“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>

现在我还需要显示订单,但我需要在列表框外显示它。像这样:

 <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上没有订单的属性。订单是客户对象上的集合。我怎样才能做到呢

以下是更新后的示例:

<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>


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

您可以使用绑定。

您可以使用绑定。

我建议您在窗口中添加一个附加列表,并将其数据上下文绑定到列表框中当前选定的客户。它将是这样的:

        <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>

我建议您在窗口中添加一个附加列表,并将其数据上下文绑定到列表框中当前选定的客户。它将是这样的:

        <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>


我更新了帖子。我关心的一件事是,为什么我必须使用ContentControl,为什么我不能只使用一个简单的控件来显示所有的信息。在上面的链接中,这个人正在使用StaticResource。我正在使用自定义嵌套列表和列表。客户对象有一个订单集合。我想我已经找到了。我不得不使用Binding ElementName,ElementName属于ListBox控件。我更新了帖子。我关心的一件事是,为什么我必须使用ContentControl,为什么我不能只使用一个简单的控件来显示所有的信息。在上面的链接中,这个人正在使用StaticResource。我正在使用自定义嵌套列表和列表。客户对象有一个订单集合。我想我已经找到了。我必须使用Binding ElementName,ElementName属于ListBox控件。