【问题标题】:How does WPF Binding navigate relationships?WPF 绑定如何导航关系?
【发布时间】:2013-06-21 21:31:19
【问题描述】:

我正在尝试通过以下方式实现主从关系:

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

但我在组合框中还有一个 项,为此我需要查看详细数据网格中显示的所有客户的所有订单。

这是 XAML 代码的 sn-p:

<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. 当组合框的SelectedItem(它是运行时的DataRowView)没有属性CustomersOrdersRelation时,数据网格中的绑定如何找到Path=SelectedItem.CustomersOrdersRelation

  2. 修改我自己的NullCustomer 类以便在我选择 时显示AllOrdersCollectionViewSource 的结果的最简单方法是什么?

【问题讨论】:

    标签: wpf data-binding binding master-detail


    【解决方案1】:
    1. DataRowView 实现了ICustomTypeDescriptor,绑定系统可能使用它来确定如何获取该属性。

    2. 还给它一个属性CustomersOrdersRelation,它返回一个CompositeCollection,其中包含所有客户订单的CollectionContainers

    【讨论】:

    • 谢谢@H.B.我根据您的回答实施了解决方案,现在效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 2013-06-29
    • 2020-05-24
    • 2010-10-18
    • 2011-04-07
    • 1970-01-01
    相关资源
    最近更新 更多