码迷,mamicode.com
首页 > Windows程序 > 详细

WPF 动态生成对象属性 (dynamic)

时间:2019-12-21 00:43:55      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:detail   mss   模拟   schedule   EDA   包含   params   try   对象   

原文:WPF 动态生成对象属性 (dynamic)

 

 

 

技术图片

项目中列行的数据 都需要动态生成 所以考虑到对象绑定  可需要一个动态生成属性的意思

缺点 加载速度会慢 很明显的慢

解决办法 尽量减轻动态属性的量~

参考文章

https://www.cnblogs.com/maomiyouai/p/3594132.html

https://www.cnblogs.com/dingli/archive/2012/06/14/2548687.html(这个没看明白 但是冥冥中让我觉得 收藏一下以后可能会用)

代码

属性类   想研究明白的 看参考文章

  1. public class NurseScheduleStatisticsModel : DynamicObject
  2. {
  3. Dictionary<string, object> Properties = new Dictionary<string, object>();
  4. public override bool TrySetMember(SetMemberBinder binder, object value)
  5. {
  6. if (!Properties.Keys.Contains(binder.Name))
  7. {
  8. Properties.Add(binder.Name, value.ToString());
  9. }
  10. return true;
  11. }
  12. public override bool TryGetMember(GetMemberBinder binder, out object result)
  13. {
  14. return Properties.TryGetValue(binder.Name, out result);
  15. }
  16. }

生成临时数据

  1. private ObservableCollection<NurseScheduleStatisticsModel> GetNameDataLlist()
  2. {
  3. dynamic model = new NurseScheduleStatisticsModel();
  4. model.Name = "市川 賞子";
  5. model.Class_ = "B";
  6. model.JOb = "リーダー";
  7. model.Num = "1";
  8. model.Num1 = "r1";
  9. model.Num2 = "R1";
  10. model.Num3 = "r1";
  11. model.Num4 = "r1";
  12. model.Num5 = "T1";
  13. dynamic model2 = new NurseScheduleStatisticsModel();
  14. model2.Name = "石田";
  15. model2.Class_ = "B";
  16. model2.JOb = "リーダー";
  17. model2.Num = "2";
  18. model2.Num1 = "r2";
  19. model2.Num2 = "R2";
  20. model2.Num3 = "r3";
  21. model2.Num4 = "r2";
  22. model2.Num5 = "T2";
  23. dynamic model3 = new NurseScheduleStatisticsModel();
  24. model3.Name = "安达 鮎美";
  25. model3.Num = "3";
  26. model3.Class_ = "B";
  27. model3.JOb = "リーダー";
  28. model3.Num1 = "r2";
  29. model3.Num2 = "R3";
  30. model3.Num3 = "r2";
  31. model3.Num4 = "r3";
  32. model3.Num5 = "T3";
  33. modelList.Add(model);
  34. modelList.Add(model2);
  35. modelList.Add(model3);
  36. return modelList;
  37. }

动态增加列和绑定datagrid

dl.Binding = new Binding("Num" + (i + 1) );

这里的bing 绑定的就是对象里的Num1~开始的对象  

  1. for (int i = 0; i < 10; i++)
  2. {
  3. DataGridTextColumn dl = new DataGridTextColumn();
  4. dl.Header=‘num‘+i;
  5. dl.Binding = new Binding("Num" + (i + 1) );
  6. dataGrid.Columns.Add(dl);
  7. }
  8. dataGrid.ItemsSource = GetNameDataLlist();

 

我在项目里 使用一个自定义对象 来包含 dynamic 动态对象 减少不必要的开销

 

对象 

  1. public class NameList : INotifyPropertyChanged
  2. {
  3. public event PropertyChangedEventHandler PropertyChanged;
  4. public NameList(string name, string jOb, string class_, int num, NurseScheduleStatisticsModel model) { Name = name; Class_ = class_; JOb = jOb; Num = num; Ml = model; }
  5. private string name;
  6. public string Name
  7. {
  8. get { return name; }
  9. set
  10. {
  11. name = value;
  12. if (PropertyChanged != null)
  13. {
  14. PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  15. }
  16. }
  17. }
  18. private int num;
  19. public int Num
  20. {
  21. get { return num; }
  22. set
  23. {
  24. num = value;
  25. if (PropertyChanged != null)
  26. {
  27. PropertyChanged(this, new PropertyChangedEventArgs("Num"));
  28. }
  29. }
  30. }
  31. private string class_;
  32. public string Class_
  33. {
  34. get { return class_; }
  35. set
  36. {
  37. class_ = value;
  38. if (PropertyChanged != null)
  39. {
  40. PropertyChanged(this, new PropertyChangedEventArgs("Class_"));
  41. }
  42. }
  43. }
  44. private string jOb;
  45. public string JOb
  46. {
  47. get { return jOb; }
  48. set
  49. {
  50. jOb = value;
  51. if (PropertyChanged != null)
  52. {
  53. PropertyChanged(this, new PropertyChangedEventArgs("JOb"));
  54. }
  55. }
  56. }
  57. private NurseScheduleStatisticsModel ml;
  58. public NurseScheduleStatisticsModel Ml
  59. {
  60. get { return ml; }
  61. set { ml = value;
  62. if (PropertyChanged != null)
  63. {
  64. PropertyChanged(this, new PropertyChangedEventArgs("Ml"));
  65. }
  66. }
  67. }
  68. }

模拟数据

  1. private ObservableCollection<NameList> GetNameDataLlist2()
  2. {
  3. dynamic model = new NurseScheduleStatisticsModel();
  4. model.Num1 = "r1";
  5. model.Num2 = "R1";
  6. model.Num3 = "r1";
  7. model.Num4 = "r1";
  8. model.Num5 = "T1";
  9. dynamic model2 = new NurseScheduleStatisticsModel();
  10. model2.Num1 = "r2";
  11. model2.Num2 = "R2";
  12. model2.Num3 = "r3";
  13. model2.Num4 = "r2";
  14. model2.Num5 = "T2";
  15. dynamic model3 = new NurseScheduleStatisticsModel();
  16. model3.Num1 = "r2";
  17. model3.Num2 = "R3";
  18. model3.Num3 = "r2";
  19. model3.Num4 = "r3";
  20. model3.Num5 = "T3";
  21. //modelList.Add(model);
  22. //modelList.Add(model2);
  23. //modelList.Add(model3);
  24. listName.Add(new NameList("市川 賞子", "リーダー", "B", 1, model));
  25. listName.Add(new NameList("石田", "リーダー", "C", 2, model2));
  26. listName.Add(new NameList("安达 鮎美", "リーダー", "C", 3, model3));
  27. return listName;
  28. }

 

绑定动态列

需要在套一层Ml对象来引用

  1. for (int i = 0; i < LS.Count; i++)
  2. {
  3. DataGridTextColumn dl = new DataGridTextColumn();
  4. dl.Header=LS[i];
  5. dl.Binding = new Binding("Ml.Num" + (i + 1) );
  6. dataGrid.Columns.Add(dl);
  7. }

 

就是慢 的有这点数据量就要4秒~6秒 loading加载提示肯定是要有了

希望有人有更好 更成熟的方法

技术图片

 

WPF 动态生成对象属性 (dynamic)

标签:detail   mss   模拟   schedule   EDA   包含   params   try   对象   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!