Hauk.Wong

天下事有难易乎

silverlight datagrid动态生成列&动态绑定


由于返回的数据表的列不是固定的,所以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, propertyType, null);
            MethodBuilder getPropMthdBldr =
                tb.DefineMethod("get_" + propertyName,
                    MethodAttributes.Public |
                    MethodAttributes.SpecialName |
                    MethodAttributes.HideBySig,
                    propertyType, 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 模拟单元格双击事件
下一篇Flex页面打印(打印可显示组建--UIComponent)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭