WPF,XAML中使用XPath与XmlDataProvider基于在ComboBox中选定的价值选择的节点 [英] WPF, using XPath in XAML with XmlDataProvider to select nodes based on selected value in 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
的组件组合框,这样它会从产品,其
节点的所有组件/组件
节点 @name
属性等于在产品组合框中当前所选的值。这可能吗?我会怎么做呢?下面是我在我的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产品名称,所以这部分很不错。这是依赖组件组合框我需要帮助。
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表达式的例子后,我发现<一个HREF =http://karlhulme.wordpress.com/2007/02/14/xpath-variabledynamic-parameters-in-wpf-binding/相对=nofollow>这篇文章给了我我的解决方案一个迂回的方式。他的例子比我更复杂,所以我没有去创建XML转换器类。我只是需要以下XAML我依赖的组件组合框:
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
是名称
我的产品组合框的。
Here, cbo_product
is the Name
of my Product ComboBox.
这篇关于WPF,XAML中使用XPath与XmlDataProvider基于在ComboBox中选定的价值选择的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!