由于返回的数据表的列不是固定的,所以webservice端的实体我们直接建立如下格式:
public class DynamicObj { public string Item_name { get; set; } //列名称 public List<string> Item_Value { get; set; } //列值集合 }
然后webservice端返回该对象的集合,也就是列的集合。silverlight做如下处理,就可以动态生成绑定了:
private voID clIEnt_Get_DynamicObjCompleted(object sender,Get_DynamicObjCompletedEventArgs e) { DynamicObj[] result = e.Result; List<Dictionary<string,string>> dataSources = new List<Dictionary<string,string>>(result[0].Item_Value.Length); for (int j = 0; j < result[0].Item_Value.Length; j++) { Dictionary<string,string> item = new Dictionary<string,string>();//数据行(Key:列名,Value:列值) for (int i = 0; i < result.Length; i++) { //列名必须符合变量命名规则 item.Add(result[i].Item_name.Replace(" ","_").Replace("-","_"),result[i].Item_Value[j]); } dataSources.Add(item); } dataGrID1.ItemsSource = GetEnumerable(dataSources).ToDataSource(); }
GetEnumerable:
public IEnumerable<IDictionary> GetEnumerable(List<Dictionary<string,string>> SourceList) { for (int i = 0; i < SourceList.Count; i++) { var dict = new Dictionary<string,string>(); dict = SourceList[i]; yIEld return dict; } }
ToDataSource为IEnumerable<IDictionary>的扩展方法,代码如下:
public static class DataSourceCreator { private static Readonly Regex PropertnameRegex = new Regex(@"^[A-Za-z]+[A-Za-z1-9_]*{1}quot;,RegexOptions.Singleline); public static List<object> ToDataSource(this IEnumerable<IDictionary> List) { IDictionary firstDict = null; bool hasData = false; foreach (IDictionary currentDict in List) { hasData = true; firstDict = currentDict; break; } if (!hasData) { return new List<object> { }; } if (firstDict == null) { throw new ArgumentException("IDictionary entry cannot be null"); } Type objectType = null; TypeBuilder tb = GetTypeBuilder(List.GetHashCode()); ConstructorBuilder constructor = tb.defineDefaultConstructor( MethodAttributes.Public | MethodAttributes.Specialname | MethodAttributes.RTSpecialname); foreach (DictionaryEntry pair in firstDict) { if (PropertnameRegex.IsMatch(Convert.ToString(pair.Key),0)) { CreateProperty(tb,Convert.ToString(pair.Key),pair.Value == null ? typeof(object) : pair.Value.GetType()); } else { throw new ArgumentException( @"Each key of IDictionary must be Alphanumeric and start with character."); } } objectType = tb.CreateType(); return GenerateArray(objectType,List,firstDict); } private static List<object> GenerateArray(Type objectType,IEnumerable<IDictionary> List,IDictionary firstDict) { var itemsSource = new List<object>(); foreach (var currentDict in List) { if (currentDict == null) { throw new ArgumentException("IDictionary entry cannot be null"); } object row = Activator.CreateInstance(objectType); foreach (DictionaryEntry pair in firstDict) { if (currentDict.Contains(pair.Key)) { PropertyInfo property = objectType.GetProperty(Convert.ToString(pair.Key)); property.SetValue( row,Convert.ChangeType( currentDict[pair.Key],property.PropertyType,null),null); } } itemsSource.Add(row); } return itemsSource; } private static TypeBuilder GetTypeBuilder(int code) { Assemblyname an = new Assemblyname("TempAssembly" + code); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.defineDynamicAssembly( an,AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.defineDynamicModule("MainModule"); TypeBuilder tb = moduleBuilder.defineType("TempType" + code,TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.autoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFIEldInit | TypeAttributes.autoLayout,typeof(object)); return tb; } private static voID CreateProperty(TypeBuilder tb,string propertyname,Type propertyType) { FIEldBuilder fIEldBuilder = tb.defineFIEld("_" + propertyname,propertyType,FIEldAttributes.Private); PropertyBuilder propertyBuilder = tb.defineProperty( propertyname,PropertyAttributes.HasDefault,null); MethodBuilder getPropMthdBldr = tb.defineMethod("get_" + propertyname,MethodAttributes.Public | MethodAttributes.Specialname | MethodAttributes.HIDeBySig,Type.EmptyTypes); ILGenerator getIL = getPropMthdBldr.GetILGenerator(); getIL.Emit(OpCodes.Ldarg_0); getIL.Emit(OpCodes.Ldfld,fIEldBuilder); getIL.Emit(OpCodes.Ret); MethodBuilder setPropMthdBldr = tb.defineMethod("set_" + propertyname,MethodAttributes.Public | MethodAttributes.Specialname | MethodAttributes.HIDeBySig,null,new Type[] { propertyType }); ILGenerator setIL = setPropMthdBldr.GetILGenerator(); setIL.Emit(OpCodes.Ldarg_0); setIL.Emit(OpCodes.Ldarg_1); setIL.Emit(OpCodes.Stfld,fIEldBuilder); setIL.Emit(OpCodes.Ret); propertyBuilder.Setgetmethod(getPropMthdBldr); propertyBuilder.SetSetMethod(setPropMthdBldr); } }总结
以上是内存溢出为你收集整理的silverlight datagrid动态生成列&动态绑定全部内容,希望文章能够帮你解决silverlight datagrid动态生成列&动态绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)