0

我有一个类似于thisthis的问题。这是一个描述:

我有以下 XML:

<Parts>
  <Part id="1" name="part1">
    <SubParts>
      <SubPart id="1" name="sub_part1"/>
      <SubPart id="2" name="sub_part2"/>
    </SubParts>
  </Part>
  ...
</Parts>

当然,我想ComboBox在我的 WPF 控件中有两个依赖项——一个用于部件,一个用于子部件。

与上述解决方案的不同之处在于,我不想替换第二个的 DataContext,ComboBox因为我失去了父 DataContext 绑定。

我想得到这样的东西:

  <ComboBox x:Name="_partCombo"  ItemsSource="{Binding Source={StaticResource xmlPartList}, XPath=./Part}"
              ...
              SelectedValue="{Binding PartID}"/>

  <ComboBox x:Name="_subPartCombo"  ItemsSource="{Binding Source={StaticResource xmlPartList}, XPath=./Part/SubParts}"
              ...
              SelectedValue="{Binding SubPartID}"/>

我尝试使用中间数据成员从第一个绑定所选项目,ComboBox但我无法从第二个绑定到它。

需要任何帮助。

4

1 回答 1

0

尝试

ItemsSource="{Binding Source={StaticResource xmlPartList}, 
                      RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window},
                      XPath=./Part/SubParts}"
于 2011-12-16T15:58:02.140 回答