WPF 之使用Treeview和DataGrid的关联绑定

本文介绍了如何在WPF应用中,通过XML数据源将内容绑定到TreeView,当在TreeView中选择节点时,相应数据自动显示在DataGrid中。详细步骤包括创建XML文件、定义Model实体、数据访问类、实现INotifyPropertyChanged接口的ViewModel以及窗体的绑定代码。项目运行后,展示了数据绑定的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现功能:WPF通过读取XML数据绑定到TreeView, 在通过TreeView选择项(Node)获取的对应的数据绑定到DataGrid控件上,再通过DataGrid选中行的详细信息数据绑定到另一个DataGrid。

首先创建XML数据. 名称为 Order.xml

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <category name="Computer">
    <product name="联想笔记本">
      <order orderId="1" orderName="cizon的订单" orderDate="2012-11-10">
        <orderInfo productName="联想笔记本" unitPrice="3000" count="2"> 
        </orderInfo>
      </order>
      <order orderId="2" orderName="steven的订单" orderDate="2012-11-10">
        <orderInfo productName="联想笔记本" unitPrice="3000" count="2">
        </orderInfo>
      </order>
    </product>
    <product name="宏基笔记本">
      <order orderId="1" orderName="Luly的订单" orderDate="2012-11-10">
        <orderInfo productName="宏基笔记本" unitPrice="2000" count="3">
        </orderInfo>
      </order>
      <order orderId="2" orderName="steven的订单" orderDate="2012-11-10">
        <orderInfo productName="宏基笔记本" unitPrice="2000" count="3">
        </orderInfo>
      </order>
    </product>
    <product name="华硕笔记本"></product>
  </category>
  <category name="TV">
    <product name="海尔电视">
      <order orderId="1" orderName="cizon的订单" orderDate="2012-11-10">
        <orderInfo productName="海尔电视" unitPrice="1000" count="2">
        </orderInfo>
      </order>
      <order orderId="2" orderName="steven的订单" orderDate="2012-11-10">
        <orderInf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值