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
- WPF下ListView 绑定xml数据源的方案
- WPF绑定XML数据源
- WPF绑定各种数据源之xml数据源
- WPF中实现ListView控件绑定到多个数据源的并集
- WPF中ListView绑定数据源后居中显示
- wpf绑定用户控件及listview(gridview)数据源
- WPF数据源绑定方式
- WPF-DataGrid 绑定数据源
- listview后台绑定数据源的用法
- 自定义适配器--ListView数据源的绑定
- 【WPF】动态绑定listview的列内容
- WPF\Silverlight 数据源绑定方式
- WPF绑定数据源之RelativeSource
- WPF绑定各种数据源之object数据源
- wpf 如何更改一个页面的绑定数据源
- WPF的ComboBox数据绑定,使用Dictionary作为数据源
- WPF-获取Combobox已绑定的数据源并转成Dictionary
- WPF listView未找到数据源
- 95后“Z一代”要成年了,准备好迎接他们了么?
- KMP - 字符串查找算法
- jquery隐藏显示元素:$('#id').css('display','none');$('#id').hide();
- 鸟哥私房菜基础篇: 第零章、計算機概論
- 用springmvc作接口时返回json数据中文乱码
- WPF下ListView 绑定xml数据源的方案
- mule学习笔记(七):expression MEL与Java
- RelativeLayout常用属性介绍及实例讲解
- 技术让我们更聪明?
- 网络请求封装
- 判断字符串是否为数字且至少包含一位字母
- jsonresult 超出范围
- mysql只导出表结构或数据
- 苹果APP加急申请