views:

79

answers:

1

This question ties to my previous question but is more specific. Say I have two ComboBoxes, one populated with product names, the other empty. When a product is chosen, I want the second ComboBox to be filled with data related to that product. I have XML like the following:

<Products>
    <Product name="MyProduct1">
        <Components>
            <Component name="MyComponent1"/>
            <Component name="MyComponent2"/>
            ...more Component nodes...
        </Components>
    </Product>
    ...more Product nodes...
</Products>

What I was hoping is that I could somehow set the XPath on the Component ComboBox such that it gets all the Components/Component nodes from the Product node whose @name attribute is equal to the currently selected value in the Product ComboBox. Is this possible? How would I do it? Here's what I have in my XAML so far:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
          Name="cbo_product" VerticalAlignment="Top" Width="120"
          ItemsSource="{Binding Source={StaticResource productsXml}}"
          DisplayMemberPath="@name"/>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,151,0,0"
            Name="cbo_component" VerticalAlignment="Top" Width="201"
            DisplayMemberPath="@name">
    <ComboBox.ItemsSource>
        <!-- Currently gets all Component nodes--want it to get only those
             associated with the selected Product -->
        <Binding Source="{StaticResource productsXml}"
                 XPath="Components/Component"/>
    </ComboBox.ItemsSource>
</ComboBox>

The Product ComboBox is filled with product names like MyProduct1, so that part is fine. It's the dependent Component ComboBox I need help with.

A: 

After Googling around for examples of XPath expressions in WPF, I found this article that gave me my solution in a round-about way. His example was more complicated than mine, so I didn't have to go creating XML converter classes. I just needed the following XAML for my dependent Component ComboBox:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,151,0,0"
          Name="cbo_component" VerticalAlignment="Top" Width="201"
          DataContext="{Binding ElementName=cbo_product, Path=SelectedItem}"
          ItemsSource="{Binding XPath=Components/Component}"
          DisplayMemberPath="@name"/>

Here, cbo_product is the Name of my Product ComboBox.

Sarah Vessels