2013-06-21 30 views
1

我试图实现以下面的方式主从关系:WPF绑定如何导航关系?

(shown in a ComboBox)      (shown in a DataGrid) 
|-----------|        |------------| 
| Customers |        | Orders  | 
|-----------|        |------------| 
| Id  |--- CustomersOrdersRelation ---| CustomerId | 
| Name  |        | OrderId | 
| ...  |        | ...  | 
|-----------|        |------------| 

但我也有一个<所有客户>项目在ComboBox,为此我需要看到所有客户的所有订单显示在详细数据网格中。

这里是XAML代码片段:

<ComboBox x:Name="CustomersComboBox" ...> 
    <ComboBox.ItemsSource> 
     <CompositeCollection> 
      <ComboBoxItem Content="{StaticResource nullCustomer}" /> <!-- I wrote my own class NullCustomer --> 
      <CollectionContainer Collection="{Binding Source={StaticResource CustomersCollectionViewSource}}" /> 
     </CompositeCollection> 
    </ComboBox.ItemsSource> 
</ComboBox> 

<DataGrid ItemsSource="{Binding ElementName=CustomersComboBox, Path=SelectedItem.CustomersOrdersRelation}" ...> 

现在我有两个问题:

  1. 如何在DataGrid中绑定发现Path=SelectedItem.CustomersOrdersRelation当组合框的SelectedItem(这是一个DataRowView在运行时)没有属性CustomersOrdersRelation

  2. 什么是我修改我自己NullCustomer类,所以,当我选择<所有客户>我得的AllOrdersCollectionViewSource显示的结果最简单的方法?

回答

1
  1. DataRowView实现ICustomTypeDescriptor,这可能是所使用的绑定系统,以确定如何获得该属性。

  2. 也给它一个属性CustomersOrdersRelation它返回CompositeCollection包含CollectionContainers为客户的所有订单。

+0

谢谢@ H.B。我根据您的答案实施了解决方案,现在它工作得很好。 –