WPF,XML数据绑定到依赖/级联组合框 [英] WPF, XML databinding into dependent/cascading ComboBoxes
问题描述
我有以下结构的XML文件:
<产品>
<产品名称=MyProduct1>
<成分>
<成分名称=MyComponent1>
<&子GT;
<子名=MySubComponent1/>
<子名=MySubComponent2/> $ B $ ...更多子节点...
< /子>
< /成分> $ B $ ...更多组件节点...
< /成分>
< /产品与GT; $ B $ ...更多产品节点...
< /产品>
我试图创建一个与它的产品名称组合框一个WPF应用程序。我完全新的WPF,所以我不知道我要去的事情以正确的方式。当选择一个产品,一个第二组合框应与所有组件为该产品来填充。并且选择一个组件时,第三组合框应与所有的子组件,该组件填充。
我不知道如何设置组合框之间的相关性,除了填充由独立的组合框触发事件处理中的相关组合框。这似乎意味着,我需要能够读取C#中的XML,所以我的 [Serializable接口]
类产品
,产品
,组件
和子
。不过,我试图做的XML数据绑定在我的XAML:
< Window.Resources>
< XmlDataProvider源=Products.xml
的XPath =产品/产品
X:键=productsXml/>
< /Window.Resources>
我目前没有看到产品名称的列表,在我的第一个组合框,它的XAML是以下
<组合框高度=23的HorizontalAlignment =左保证金=138,116,0,0
产品名称=cbo_productVerticalAlignment =评出的WIDTH =120
的ItemsSource ={绑定源= productsXml,XPath的= @名}
的SelectionChanged =product_SelectionChanged/>
产品XML应该只读 - 用户将无法更改任何值从应用程序的XML。我只是想读取XML数据,并在应用中展示它
我有几个问题:
- 我要对这个正确?有从我的WPF应用程序读取,有一个单独的XML文件
[Serializable接口]
代表在C#中提取这些节点数据的目的XML文件中的节点,使用类事件处理程序组合框之间的代码的依赖等。 - 为什么我的产品名称,例如,MyProduct1,在我的组合框显示?目前,它只是表明了空。
- 看来几乎就像
[Serializable接口]
类代表我的XML节点是多余的/不必要的,因为XAML已经拥有了XmlDataProvider
/ XPath的东西。 ?这样的话
编辑:
更新我的组合框的XAML以下,现在我看到我的产品名称列表在ComboBox,感谢的 decyclone的回答:
<组合框高度=23的HorizontalAlignment =左保证金=138,116,0,0
NAME =cbo_productVerticalAlignment =评出的WIDTH =120
的ItemsSource ={绑定源= {StaticResource的productsXml}}
的DisplayMemberPath =@名
的SelectionChanged =product_SelectionChanged/>
好吧,因为我找到了答案,我的<一个HREF =http://stackoverflow.com/questions/3389333/wpf-using-xpath-in-xaml-with-xmldataprovider-to-select-nodes-based-on-selected-v/>更具体的问题一>,我想我知道这个问题的答案了。我不需要在不同的节点 [Serializable接口]
班在我的XML,因为我可以只使用XAML和XPath创建级联/取决于组合框:
<! - 独立 - >
<组合框高度=23的HorizontalAlignment =左保证金=138,116,0,0
NAME =cbo_productVerticalAlignment =评出的WIDTH =120
的ItemsSource ={绑定源= {StaticResource的productsXml}}
的DisplayMemberPath =@名/>
<! - 依赖 - >
<组合框高度=23的HorizontalAlignment =左保证金=138,151,0,0
NAME =cbo_componentVerticalAlignment =评出的WIDTH =201
的DataContext ={绑定的ElementName = cbo_product,路径=的SelectedItem}
的ItemsSource ={绑定的XPath =组件/组件}
的DisplayMemberPath =@名/>
I have an XML file with the following structure:
<Products>
<Product name="MyProduct1">
<Components>
<Component name="MyComponent1">
<SubComponents>
<SubComponent name="MySubComponent1"/>
<SubComponent name="MySubComponent2"/>
...more SubComponent nodes...
</SubComponents>
</Component>
...more Component nodes...
</Components>
</Product>
...more Product nodes...
</Products>
I'm trying to create a WPF app that has a ComboBox with the Product names in it. I am completely new to WPF so I don't know if I'm going about things the right way. When a Product is chosen, a second ComboBox should be populated with all the Components for that Product. And when a Component is chosen, a third ComboBox should be populated with all the SubComponents for that Component.
I don't know how to set up a dependency between ComboBoxes except to populate the dependent ComboBox inside an event handler triggered by the independent ComboBox. This seems to imply I need to be able to read the XML in C#, so I have [Serializable]
classes for Products
, Product
, Component
, and SubComponent
. However, I was trying to do XML databinding in my XAML:
<Window.Resources>
<XmlDataProvider Source="Products.xml"
XPath="Products/Product"
x:Key="productsXml"/>
</Window.Resources>
I'm currently not seeing a list of Product names in my first ComboBox, whose XAML is the following:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
Name="cbo_product" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Source=productsXml, XPath=@name}"
SelectionChanged="product_SelectionChanged"/>
The Products XML should be read-only--the user won't be able to change any values in the XML from the app. I just want to read the XML data and display it in the app.
I have a few questions:
- Am I going about this correctly? Having a standalone XML file from which my WPF app reads, having
[Serializable]
classes representing the nodes in the XML file for the purpose of extracting data from those nodes in C#, using an event handler to code dependencies between ComboBoxes, etc. - Why don't my product names, e.g., MyProduct1, show up in my ComboBox? Currently it just shows up empty.
- It seems almost like having
[Serializable]
classes for representing my XML nodes is redundant/unnecessary since XAML already has theXmlDataProvider
/XPath stuff. Is this the case?
Edit:
Updated my ComboBox XAML to the following and now I see my list of Product names in the ComboBox, thanks to decyclone's answer:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
Name="cbo_product" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Source={StaticResource productsXml}}"
DisplayMemberPath="@name"
SelectionChanged="product_SelectionChanged"/>
Okay, since I found an answer to my more specific question, I think I know the answer to this question, too. I don't need [Serializable]
classes for the different nodes in my XML, because I can just use XAML and XPath to create cascading/dependent ComboBoxes:
<!-- Independent -->
<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
Name="cbo_product" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Source={StaticResource productsXml}}"
DisplayMemberPath="@name"/>
<!-- Dependent -->
<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"/>
这篇关于WPF,XML数据绑定到依赖/级联组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!