WPF,使用XAML中的XPath与XmlDataProvider根据ComboBox中的选定值选择节点 [英] WPF, using XPath in XAML with XmlDataProvider to select nodes based on selected value in ComboBox

查看:174
本文介绍了WPF,使用XAML中的XPath与XmlDataProvider根据ComboBox中的选定值选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与我的上一个问题有关,但更具体。说我有两个组合框,一个填充了产品名称,另一个是空的。当选择产品时,我希望第二个ComboBox填充与该产品相关的数据。我有如下的XML:

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>

我希望能够以某种方式设置 XPath 组件ComboBox,以便它从 Product 节点中获取所有组件/组件节点, code> @name 属性等于Product ComboBox中当前选定的值。这可能吗?我该怎么办?这是我到目前为止在我的XAML中有的:

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>

产品组合框中填写了产品名称,如MyProduct1,因此部分是好的。这是依赖的组件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.

推荐答案

在Googling之后,在WPF中的XPath表达式的例子,我发现这篇文章给了我我的解决方案以一轮四射的方式。他的例子比我的复杂,所以我不必去创建XML转换器类。我只需要以下XAML作为我的依赖组件ComboBox:

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"/>

这里, cbo_product 我的产品组合框的名称

这篇关于WPF,使用XAML中的XPath与XmlDataProvider根据ComboBox中的选定值选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆