WPF 动态生成对象属性 (dynamic)
Posted lonelyxmas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 动态生成对象属性 (dynamic)相关的知识,希望对你有一定的参考价值。
原文:WPF 动态生成对象属性 (dynamic)
项目中列行的数据 都需要动态生成 所以考虑到对象绑定 可需要一个动态生成属性的意思
缺点 加载速度会慢 很明显的慢
解决办法 尽量减轻动态属性的量~
参考文章
https://www.cnblogs.com/maomiyouai/p/3594132.html
https://www.cnblogs.com/dingli/archive/2012/06/14/2548687.html(这个没看明白 但是冥冥中让我觉得 收藏一下以后可能会用)
代码
属性类 想研究明白的 看参考文章
- public class NurseScheduleStatisticsModel : DynamicObject
- {
-
-
- Dictionary<string, object> Properties = new Dictionary<string, object>();
-
- public override bool TrySetMember(SetMemberBinder binder, object value)
- {
- if (!Properties.Keys.Contains(binder.Name))
- {
-
- Properties.Add(binder.Name, value.ToString());
- }
- return true;
- }
- public override bool TryGetMember(GetMemberBinder binder, out object result)
- {
- return Properties.TryGetValue(binder.Name, out result);
- }
- }
生成临时数据
- private ObservableCollection<NurseScheduleStatisticsModel> GetNameDataLlist()
- {
-
- dynamic model = new NurseScheduleStatisticsModel();
- model.Name = "市川 賞子";
- model.Class_ = "B";
- model.JOb = "リーダー";
- model.Num = "1";
- model.Num1 = "r1";
- model.Num2 = "R1";
- model.Num3 = "r1";
- model.Num4 = "r1";
- model.Num5 = "T1";
- dynamic model2 = new NurseScheduleStatisticsModel();
- model2.Name = "石田";
- model2.Class_ = "B";
- model2.JOb = "リーダー";
- model2.Num = "2";
- model2.Num1 = "r2";
- model2.Num2 = "R2";
- model2.Num3 = "r3";
- model2.Num4 = "r2";
- model2.Num5 = "T2";
- dynamic model3 = new NurseScheduleStatisticsModel();
- model3.Name = "安达 鮎美";
- model3.Num = "3";
- model3.Class_ = "B";
- model3.JOb = "リーダー";
- model3.Num1 = "r2";
- model3.Num2 = "R3";
- model3.Num3 = "r2";
- model3.Num4 = "r3";
- model3.Num5 = "T3";
-
- modelList.Add(model);
- modelList.Add(model2);
- modelList.Add(model3);
-
-
-
- return modelList;
-
- }
动态增加列和绑定datagrid
dl.Binding = new Binding("Num" + (i + 1) );
这里的bing 绑定的就是对象里的Num1~开始的对象
- for (int i = 0; i < 10; i++)
- {
- DataGridTextColumn dl = new DataGridTextColumn();
- dl.Header=‘num‘+i;
-
- dl.Binding = new Binding("Num" + (i + 1) );
- dataGrid.Columns.Add(dl);
- }
- dataGrid.ItemsSource = GetNameDataLlist();
我在项目里 使用一个自定义对象 来包含 dynamic 动态对象 减少不必要的开销
对象
- public class NameList : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
-
- public NameList(string name, string jOb, string class_, int num, NurseScheduleStatisticsModel model) { Name = name; Class_ = class_; JOb = jOb; Num = num; Ml = model; }
-
-
- private string name;
-
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Name"));
- }
-
- }
- }
- private int num;
-
- public int Num
- {
- get { return num; }
- set
- {
- num = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Num"));
- }
- }
- }
- private string class_;
-
- public string Class_
- {
- get { return class_; }
- set
- {
- class_ = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Class_"));
- }
- }
- }
-
-
-
- private string jOb;
-
- public string JOb
- {
- get { return jOb; }
- set
- {
- jOb = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("JOb"));
- }
- }
- }
-
- private NurseScheduleStatisticsModel ml;
-
- public NurseScheduleStatisticsModel Ml
- {
- get { return ml; }
- set { ml = value;
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs("Ml"));
- }
- }
- }
-
-
-
- }
模拟数据
- private ObservableCollection<NameList> GetNameDataLlist2()
- {
-
- dynamic model = new NurseScheduleStatisticsModel();
-
- model.Num1 = "r1";
- model.Num2 = "R1";
- model.Num3 = "r1";
- model.Num4 = "r1";
- model.Num5 = "T1";
- dynamic model2 = new NurseScheduleStatisticsModel();
-
- model2.Num1 = "r2";
- model2.Num2 = "R2";
- model2.Num3 = "r3";
- model2.Num4 = "r2";
- model2.Num5 = "T2";
- dynamic model3 = new NurseScheduleStatisticsModel();
-
- model3.Num1 = "r2";
- model3.Num2 = "R3";
- model3.Num3 = "r2";
- model3.Num4 = "r3";
- model3.Num5 = "T3";
-
- //modelList.Add(model);
- //modelList.Add(model2);
- //modelList.Add(model3);
-
- listName.Add(new NameList("市川 賞子", "リーダー", "B", 1, model));
- listName.Add(new NameList("石田", "リーダー", "C", 2, model2));
- listName.Add(new NameList("安达 鮎美", "リーダー", "C", 3, model3));
-
- return listName;
-
- }
绑定动态列
需要在套一层Ml对象来引用
- for (int i = 0; i < LS.Count; i++)
- {
- DataGridTextColumn dl = new DataGridTextColumn();
- dl.Header=LS[i];
-
- dl.Binding = new Binding("Ml.Num" + (i + 1) );
- dataGrid.Columns.Add(dl);
- }
就是慢 的有这点数据量就要4秒~6秒 loading加载提示肯定是要有了
希望有人有更好 更成熟的方法
以上是关于WPF 动态生成对象属性 (dynamic)的主要内容,如果未能解决你的问题,请参考以下文章
Wpf DataGrid 绑定Dynamic (二维数据表)
Wpf DataGrid 绑定Dynamic (二维数据表)