WPF,XML数据绑定到依赖/级联ComboBox [英] WPF, XML databinding into dependent/cascading ComboBoxes

查看:236
本文介绍了WPF,XML数据绑定到依赖/级联ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的XML文件:

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>

我正在尝试创建一个包含产品名称的ComboBox的WPF应用程序。我完全是WPF的新手,所以我不知道我是否正在做正确的事情。选择产品时,应使用该产品的所有组件填充第二个ComboBox。当选择一个组件时,第三个ComboBox应该填充该组件的所有子组件。

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.

我不知道如何在ComboBox之间设置依赖关系,在由独立的ComboBox触发的事件处理程序中填充依赖的ComboBox。这似乎意味着我需要能够读取C#中的XML,所以我有一个 [Serializable] 课程产品产品组件 SubComponent 。但是,我正在尝试在我的XAML中进行XML数据绑定:

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>

我目前没有看到我的第一个ComboBox中的产品名称列表,其XAML是以下:

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

产品XML应为只读 - 用户将无法更改任何值在应用程序的XML中。我只想阅读XML数据并将其显示在应用程序中。

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.

我有几个问题:


  1. 我正在这样做吗?拥有一个独立的XML文件,我的WPF应用程序读取,具有表示XML文件中的节点的 [Serializable] 类,用于从C#中的这些节点提取数据,使用一个事件处理程序来编码ComboBoxes之间的依赖关系等。

  2. 为什么我的产品名称(例如MyProduct1)不显示在我的ComboBox中?目前它只是显示为空。

  3. 似乎几乎就像有 [Serializable] 表示我的XML节点的类是多余的/不必要的XAML已经具有 XmlDataProvider / XPath的东西。是这样吗?

  1. 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.
  2. Why don't my product names, e.g., MyProduct1, show up in my ComboBox? Currently it just shows up empty.
  3. It seems almost like having [Serializable] classes for representing my XML nodes is redundant/unnecessary since XAML already has the XmlDataProvider/XPath stuff. Is this the case?

编辑:

将我的ComboBox XAML更新到以下内容,现在我在ComboBox中看到我的产品名称列表,感谢 decyclone's answer

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


推荐答案

好的,因为我找到了一个答案href =http://stackoverflow.com/questions/3389333/wpf-using-xpath-in-xaml-with-xmldataprovider-to-select-nodes-based-on-selected-v/>更具体的问题,我想我也知道这个问题的答案。我不需要XML中的不同节点的 [Serializable] 类,因为我可以使用XAML和XPath来创建级联/依赖的ComboBox:

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数据绑定到依赖/级联ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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