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

来源:互联网 发布:网络拓扑图图标 编辑:程序博客网 时间:2024/06/11 15:59

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


即类似如下的数据源

<?xml version="1.0" encoding="gb2312"?><items>  <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="进行中"/>  <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="未发布"/>  <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="未发布"/>  <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="已结束"/>  <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="未开始"/>  <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="进行中"/>  <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="已结束"/></items>




主窗体:

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


核心代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Xml.Linq;namespace TestService2{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        Dictionary<string, string> oNameMapping = new Dictionary<string, string>();        public MainWindow()        {            oNameMapping.Add("id", "主键");            oNameMapping.Add("procode", "产品编号");            oNameMapping.Add("entrustunit", "委托单位ID");            oNameMapping.Add("entrustunitname", "委托单位名称");            oNameMapping.Add("appler", "申请人");            oNameMapping.Add("entrustdate", "委托日期");            oNameMapping.Add("principal", "任务负责人ID");            oNameMapping.Add("principalname", "任务负责人名称");            oNameMapping.Add("proname", "产品名称");            oNameMapping.Add("pronum", "产品数量");            InitializeComponent();            GridView tempGrid = (GridView)this.listView1.View;            foreach (string strKey in oNameMapping.Keys)            {                GridViewColumn gvc = new GridViewColumn();                gvc.Header = oNameMapping[strKey];                Binding binding = new Binding();                //binding.Path = new PropertyPath([strKey]);                //binding.XPath = "@" + strKey;  并不是以XPath方式绑定                binding.Path = new PropertyPath("Attribute[" + strKey + "].Value");                   gvc.DisplayMemberBinding = binding;                tempGrid.Columns.Add(gvc);            }        }        private void button1_Click(object sender, RoutedEventArgs e)        {            TestService2.Service.WebServiceSoapClient web = new Service.WebServiceSoapClient();            string strMessage = string.Empty;            string strXml = web.GetInfos(out strMessage);            XElement oItems = XElement.Parse(strXml);                     this.listView1.ItemsSource = oItems.Descendants("item");        }    }}

最终效果:



0 0
原创粉丝点击