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

WPF Datagrid 动态生成列 并绑定数据

时间:2019-12-20 23:55:33      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:col   数据绑定   get   动态生成对象   obs   https   void   ext   怎么   

原文:WPF Datagrid 动态生成列 并绑定数据

技术图片

说的是这里 因为列头是动态加载的 (后台for循环 一会能看到代码) 

 数据来源于左侧列 

左侧列数据源 当然num1 属于临时的dome使用  可以用ObservableCollection集合代表 动态创建属性

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

  1. ObservableCollection<NameList> listName = new ObservableCollection<NameList>();
  2. private ObservableCollection<NameList> GetNameData()
  3. {
  4. listName.Add(new NameList("市川 賞子", "リーダー", "B", 1, "2", "14", "r1", "R5", "T6"));
  5. listName.Add(new NameList("石田", "リーダー", "C", 2, "33", "T4", "r2", "R5", "T6"));
  6. listName.Add(new NameList("安达 鮎美", "リーダー", "C", 3,"3","4","r1","R6","T6"));
  7. return listName;
  8. }
  9. }
  10. public class NameList : INotifyPropertyChanged
  11. {
  12. public event PropertyChangedEventHandler PropertyChanged;
  13. public NameList(string name, string jOb, string class_, int num, string n1, string n2, string n3, string n4, string n5) { Name = name; Class_ = class_; JOb = jOb; Num = num; Num1 = n1; Num2 = n2; Num3 = n3; Num4 = n4; Num5 = n5; }
  14. private string name;
  15. public string Name
  16. {
  17. get { return name; }
  18. set
  19. {
  20. name = value;
  21. if (PropertyChanged != null)
  22. {
  23. PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  24. }
  25. }
  26. }
  27. private int num;
  28. public int Num
  29. {
  30. get { return num; }
  31. set
  32. {
  33. num = value;
  34. if (PropertyChanged != null)
  35. {
  36. PropertyChanged(this, new PropertyChangedEventArgs("Num"));
  37. }
  38. }
  39. }
  40. private string class_;
  41. public string Class_
  42. {
  43. get { return class_; }
  44. set
  45. {
  46. class_ = value;
  47. if (PropertyChanged != null)
  48. {
  49. PropertyChanged(this, new PropertyChangedEventArgs("Class_"));
  50. }
  51. }
  52. }
  53. private string jOb;
  54. public string JOb
  55. {
  56. get { return jOb; }
  57. set
  58. {
  59. jOb = value;
  60. if (PropertyChanged != null)
  61. {
  62. PropertyChanged(this, new PropertyChangedEventArgs("JOb"));
  63. }
  64. }
  65. }
  66. private string num1;
  67. public string Num1
  68. {
  69. get { return num1; }
  70. set { num1 = value;
  71. if (PropertyChanged != null)
  72. {
  73. PropertyChanged(this, new PropertyChangedEventArgs("Num1"));
  74. }
  75. }
  76. }
  77. private string num2;
  78. public string Num2
  79. {
  80. get { return num2; }
  81. set { num2 = value;
  82. if (PropertyChanged != null)
  83. {
  84. PropertyChanged(this, new PropertyChangedEventArgs("Num2"));
  85. }
  86. }
  87. }
  88. private string num3;
  89. public string Num3
  90. {
  91. get { return num3; }
  92. set { num3 = value;
  93. if (PropertyChanged != null)
  94. {
  95. PropertyChanged(this, new PropertyChangedEventArgs("Num3"));
  96. }
  97. }
  98. }
  99. private string num4;
  100. public string Num4
  101. {
  102. get { return num4; }
  103. set { num4 = value;
  104. if (PropertyChanged != null)
  105. {
  106. PropertyChanged(this, new PropertyChangedEventArgs("Num4"));
  107. }
  108. }
  109. }
  110. private string num5;
  111. public string Num5
  112. {
  113. get { return num5; }
  114. set { num5 = value;
  115. if (PropertyChanged != null)
  116. {
  117. PropertyChanged(this, new PropertyChangedEventArgs("Num5"));
  118. }
  119. }
  120. }
  121. }

列数据动态生成 与数据绑定

  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. addColumn();
  5. dataGrid.ItemsSource = GetNameData();
  6. }
  7. List<string> LS = new List<string>();
  8. public void addColumn()
  9. {
  10. LS.Add("表下カップ綿天竺仮縫い_37s_C_1");
  11. LS.Add("上カップマーキしつけ_28s_C_2");
  12. LS.Add("上下カップ接ぎ_33s_C_3");
  13. LS.Add("上下カップ押え_62s_B_4");
  14. LS.Add("カップ脇しつけ_14s_B_5");
  15. LS.Add("表上カップレース端押さえ_41s_B_6");
  16. for (int i = 0; i < LS.Count; i++)
  17. {
  18. DataGridTextColumn dl = new DataGridTextColumn();
  19. dl.Header=LS[i];
  20. dl.Binding = new Binding("Num" + (i + 1) );
  21. dataGrid.Columns.Add(dl);
  22. }
  23. }

主要是 bingding 这一行 

需要知道这俩块怎么做的朋友 可以看连接

技术图片

WPF (DataGridColumnHeader)实现自义定列头样式 并绑定数据

WPF (DataGridRowHeaderStyle)实现自义定行样式 并绑定数据

WPF Datagrid 动态生成列 并绑定数据

标签:col   数据绑定   get   动态生成对象   obs   https   void   ext   怎么   

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

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