WPF下ListView 绑定xml数据源的方案

前端之家收集整理的这篇文章主要介绍了WPF下ListView 绑定xml数据源的方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对于ListView的数据源是Xml的情况很多见,如果Xml是以元素为主封装数据源应该容易理解,这里提供的是xml是以属性为主的形式下,并且所有列动态生成的绑定方案


即类似如下的数据源

  1. <?xml version="1.0" encoding="gb2312"?>
  2. <items>
  3. <item id="18" procode="122" entrustunit="1" entrustunitname="123" appler="123321" entrustdate="2014-8-13 0:00:00" principal="0" principalname="admin" proname="312" pronum="122" proorder="232" protype="312" testtype="3" testtypename=" " taskstate="3" taskstatename="进行中"/>
  4. <item id="16" procode="vreter" entrustunit="1" entrustunitname="123" appler="cell" entrustdate="2014-8-12 0:00:00" principal="" principalname="" proname="er" pronum="12" proorder="" protype="er" testtype="3" testtypename=" " taskstate="1" taskstatename="未发布"/>
  5. <item id="12" procode="4" entrustunit="1" entrustunitname="123" appler="飞" entrustdate="2014-8-12 0:00:00" principal="" principalname="" proname="4" pronum="4" proorder="" protype="" testtype="3" testtypename=" " taskstate="1" taskstatename="未发布"/>
  6. <item id="11" procode="232" entrustunit="1" entrustunitname="123" appler="232" entrustdate="2014-8-12 0:00:00" principal="0" principalname="admin" proname="33" pronum="123" proorder="233" protype="24" testtype="1" testtypename=" " taskstate="4" taskstatename="已结束"/>
  7. <item id="5" procode="阿斯达" entrustunit="1" entrustunitname="123" appler="阿斯达" entrustdate="2014-8-6 0:00:00" principal="" principalname="" proname="123123" pronum="12" proorder="阿斯达" protype="" testtype="2" testtypename=" " taskstate="2" taskstatename="未开始"/>
  8. <item id="4" procode="12" entrustunit="1" entrustunitname="123" appler="12" entrustdate="2014-8-6 0:00:00" principal="" principalname="" proname="12" pronum="" proorder="1" protype="" testtype="2" testtypename=" " taskstate="3" taskstatename="进行中"/>
  9. <item id="1" procode="312" entrustunit="1" entrustunitname="123" appler="12" entrustdate="2014-8-4 0:00:00" principal="0" principalname="admin" proname="123" pronum="12" proorder="12" protype="123" testtype="1" testtypename=" " taskstate="4" taskstatename="已结束"/>
  10. </items>




主窗体:

  1. <Window x:Class="TestService2.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="468" Width="1300">
  5. <Grid Height="410" Width="1265">
  6. <Button Content="绑定数据" Height="23" HorizontalAlignment="Left" Margin="30,12,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
  7. <ListView Height="331" HorizontalAlignment="Left" Margin="12,57,0" Name="listView1" VerticalAlignment="Top" Width="1230">
  8. <ListView.View>
  9. <GridView AllowsColumnReorder="True"/>
  10. </ListView.View>
  11. </ListView>
  12. </Grid>
  13. </Window>


核心代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Xml.Linq;
  15. namespace TestService2
  16. {
  17. /// <summary>
  18. /// MainWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22.  
  23. Dictionary<string,string> oNameMapping = new Dictionary<string,string>();
  24.  
  25. public MainWindow()
  26. {
  27. oNameMapping.Add("id","主键");
  28. oNameMapping.Add("procode","产品编号");
  29. oNameMapping.Add("entrustunit","委托单位ID");
  30. oNameMapping.Add("entrustunitname","委托单位名称");
  31. oNameMapping.Add("appler","申请人");
  32. oNameMapping.Add("entrustdate","委托日期");
  33. oNameMapping.Add("principal","任务负责人ID");
  34. oNameMapping.Add("principalname","任务负责人名称");
  35. oNameMapping.Add("proname","产品名称");
  36. oNameMapping.Add("pronum","产品数量");
  37.  
  38.  
  39. InitializeComponent();
  40. GridView tempGrid = (GridView)this.listView1.View;
  41.  
  42. foreach (string strKey in oNameMapping.Keys)
  43. {
  44. GridViewColumn gvc = new GridViewColumn();
  45. gvc.Header = oNameMapping[strKey];
  46.  
  47. Binding binding = new Binding();
  48. //binding.Path = new PropertyPath([strKey]);
  49. //binding.XPath = "@" + strKey; 并不是以XPath方式绑定
  50.  
  51. binding.Path = new PropertyPath("Attribute[" + strKey + "].Value");
  52. gvc.DisplayMemberBinding = binding;
  53. tempGrid.Columns.Add(gvc);
  54. }
  55. }
  56.  
  57. private void button1_Click(object sender,RoutedEventArgs e)
  58. {
  59. TestService2.Service.WebServiceSoapClient web = new Service.WebServiceSoapClient();
  60. string strMessage = string.Empty;
  61. string strXml = web.GetInfos(out strMessage);
  62.  
  63. XElement oItems = XElement.Parse(strXml);
  64. this.listView1.ItemsSource = oItems.Descendants("item");
  65. }
  66. }
  67. }

最终效果


猜你在找的XML相关文章