是在参看了这篇文章 后 觉得不错,然后修改成用linq方法来实现的,全部代码如下
using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using System.Xml.linq;namespace webStudy{ public partial class XML2DataGird : UserControl { public XML2DataGird() { InitializeComponent(); string XMLStr = @" <NewDataSet> <table tablename='City' tableShowname='城市' > <Column name='Cityname' Showname='城市名称' /> <Column name='CityTel' Showname='城市区号' /> <Column name='CityCounty' Showname='城市所属国家' /> </table> <table tablename='User' tableShowname='用户' > <Column name='Username' Showname='用户名' /> <Column name='UserPwd' Showname='用户密码' /> <Column name='UserTel' Showname='用户电话' /> <Column name='UserEmail' Showname='用户邮箱' /> </table> </NewDataSet>"; Xdocument doc = Xdocument.Parse(XMLStr); var Element = from t in doc.Descendants("table") select new entitytable { tablename = t.Attribute("tablename").Value,tableShowname = t.Attribute("tableShowname").Value,eColumns = from c in t.Descendants("Column") select new entityColumn { name = c.Attribute("name").Value,showname = c.Attribute("Showname").Value },}; foreach (var item in Element) { DataGrID dgrID = new DataGrID(); dgrID.HorizontalAlignment = HorizontalAlignment.left; dgrID.VerticalAlignment = VerticalAlignment.top; dgrID.margin = new Thickness(20,5,0); dgrID.WIDth = 960; dgrID.name = item.tablename; foreach (var item1 in item.eColumns) { DataGrIDTextColumn col = new DataGrIDTextColumn(); col.header = item1.showname; dgrID.Columns.Add(col); } mainPanel.Children.Add(dgrID); } } } public class entitytable { public string tablename { get; set; } public string tableShowname { get; set; } public IEnumerable<entityColumn> eColumns { get; set; } } public class entityColumn { public string name { get; set; } public string showname { get; set; } }}总结
以上是内存溢出为你收集整理的SilverLight ---动态生成DataGrid,动态绑定DataGrid模板列--With LinQ全部内容,希望文章能够帮你解决SilverLight ---动态生成DataGrid,动态绑定DataGrid模板列--With LinQ所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)