Silverlight日记:动态生成DataGrid、行列装换、动态加载控件
2014-08-27 16:05
246 查看
本文主要针对使用DataGrid动态绑定数据对象,并实现行列转换效果。
一,前台绑定
二,动态列对象
三,DataGrid动态生成类
View Code
五,显示效果
一,前台绑定
<sdk:DataGrid x:Name="dataGrid2" Style="{StaticResource ResourceKey=safeDataGrid2}" />
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Net; using System.Reflection; using System.Reflection.Emit; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Threading; using Test.Util; namespace Test { public partial class MainPage : UserControl { public Md Model = null; private DispatcherTimer timer; int index = 0; /// <summary> /// /// </summary> public ObservableCollection<Obj> objs = new ObservableCollection<Obj>(); public MainPage() { InitializeComponent(); init(); timer = new DispatcherTimer(); timer.Interval = new TimeSpan(0, 0, 8); timer.Tick += timer_Tick; timer.Start(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void timer_Tick(object sender, EventArgs e) { var ramd = new Random(); foreach (var tag in Model.Tags) { foreach (var para in Consts.Params) { var rv = index % 2 == 0 ? ramd.Next(2, 10) : ramd.Next(1, 5); var o = new Obj { IsAlarm = false, Value = (0.5 + rv).ToString(), State = index % 2 == 0 ? 1 : 0, MeterId = tag.F_METER_ID, MeterName = tag.F_METER_NAME, ParaId = para.Id }; if (para.Id == "10") { o.IsAlarm = false; o.ParaId = string.Empty; o.State = 0; o.Value = string.Empty; } var temp = objs.FirstOrDefault(i => i.MeterName.Equals(o.MeterName) && i.ParaId == o.ParaId); if (temp != null) { temp.IsAlarm = o.IsAlarm; temp.State = o.State; temp.Value = o.Value; } else objs.Add(o); } } //CommHelper.GetSource(Model.Tags, objs, ref this.dataGrid2); CommHelper.BindData(Model.Tags, objs, ref this.dataGrid2); index++; } /// <summary> /// /// </summary> void init() { Model = new Md(); Model.Tags = new List<T_METER>(); Model.Eles = new List<Param>(); for (int i = 0; i < 1; i++) { var temp = new T_METER { F_BUILD_ID = "440300B059", F_METER_ID = string.Format("TAG_{0}", i), F_METER_NAME = string.Format("CM-{0}", i) }; if (i == 0) temp.F_EQT_TYPE = "D"; else if (i == 1) temp.F_EQT_TYPE = "C"; else if (i == 2) temp.F_EQT_TYPE = "E"; else temp.F_EQT_TYPE = "A"; Model.Tags.Add(temp); } this.dataGrid1.ItemsSource = Consts.Params; CommHelper.CreateDataGrid(Model.Tags, ref this.dataGrid2); } } /// <summary> /// /// </summary> public class Md { /// <summary> /// 参数集合 /// </summary> public List<Param> Eles { get; set; } /// <summary> /// 电表集合 /// </summary> public List<T_METER> Tags { get; set; } } /// <summary> /// /// </summary> public class T_METER { public string F_BUILD_ID { get; set; } public string F_METER_ID { get; set; } public string F_METER_NAME { get; set; } public string F_EQT_TYPE { get; set; } public string F_VALUE { get; set; } } /// <summary> /// 参数 /// </summary> public class Param { public string Id { get; set; } public string Name { get; set; } } /// <summary> /// 绑定数据对象 /// </summary> public class Obj { /// <summary> /// 对应参数编号 /// </summary> public string ParaId { get; set; } public string MeterId { get; set; } /// <summary> /// 电表名称 /// </summary> public string MeterName { get; set; } /// <summary> /// 开关状态0-关,1-开 /// </summary> public int State { get; set; } /// <summary> /// OPC读取值 /// </summary> public string Value { get; set; } /// <summary> /// 是否报警 /// </summary> public bool IsAlarm { get; set; } } }
二,动态列对象
using System.Collections.Generic; namespace Test.Util { /* ============================== * Desc:DynamicColumn * Author:hezp * Date:2014/8/14 15:49:17 * ==============================*/ public class DynamicColumn { /// <summary> /// 列名 /// </summary> public string ColumnName { get; set; } /// <summary> /// 列对应集合 /// </summary> public List<string> Values { get; set; } } }
三,DataGrid动态生成类
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; using System.Reflection.Emit; namespace Test.Util { /* ============================== * Desc:DataSourceCreator * Author:hezp * Date:2014/8/14 15:51:04 * ==============================*/ public static class DataSourceCreator { public static ObservableCollection<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 ObservableCollection<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) { CreateProperty(tb, Convert.ToString(pair.Key), pair.Value == null ? typeof(object) : pair.Value.GetType()); } objectType = tb.CreateType(); return GenerateArray(objectType, list, firstDict); } private static ObservableCollection<object> GenerateArray(Type objectType, IEnumerable<IDictionary> list, IDictionary firstDict) { var itemsSource = new ObservableCollection<object>(); foreach (var currentDict in list) { 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); } } }
View Code
五,显示效果
相关文章推荐
- Silverlight DataGrid 模板列根据内容动态生成控件
- Silverlight DataGrid 模板列根据内容动态生成控件
- [Silverlight]TreeView控件——动态加载子项
- ASP.NET动态加载用户控件的页面生成过程
- Silverlight 在页面上动态生成控件
- ASP.NET动态加载用户控件的页面生成过程
- ASP.NET动态加载用户控件的页面生成过程
- Silverlight动态生成控件实例
- Silverlight 4 DataGrid动态生成列及绑定数据
- SilverLight动态加载控件<转>
- Silverlight 4 DataGrid动态生成列及绑定数据
- SolpartMenu的使用:(二)、在ASP.NET页面中使用SolpartMenu控件之动态的加载数据库中的数据来生成菜单
- Silverlight实用窍门系列:10.动态生成DataGrid,动态绑定DataGrid模板列【附带实例源码】
- SilverLight动态加载控件
- Silverlight动态生成DataGrid的几个方法
- silverlight datagrid动态生成列&动态绑定
- silverlight DataGrid模板列的动态生成
- Easyui-Datagrid实例-----动态加载数据生成Datagrid
- ASP.NET动态加载用户控件的页面生成过程
- 动态生成advancedDataGrid并动态绑定itemRenderer改变单元格行列颜色的例子