【问题标题】:MVVM WPF Creating Child ElementsMVVM WPF 创建子元素
【发布时间】:2009-07-10 15:30:30
【问题描述】:

我有一个包含订单列表的客户对象。现在,使用 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>

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

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    您可以使用master-detail 绑定。

    【讨论】:

    • 我更新了帖子。我担心的一件事是为什么我必须使用 ContentControl 为什么我不能只使用一个简单的控件来显示所有信息。
    • 同样在上面的链接中,这个人正在使用静态资源。我正在使用自定义嵌套 List 和 List。 Customer 对象有一个 Orders 集合。
    • 我想我明白了。我必须使用 Binding ElementName 并且 ElementName 属于 ListBox 控件。
    【解决方案2】:

    我建议您在窗口中添加一个额外的列表,并将其 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2011-03-07
      相关资源
      最近更新 更多