SearchLookUpEditExtension.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraEditors.Repository;
  3. using DevExpress.XtraGrid.Columns;
  4. using DevExpress.XtraGrid.Views.Grid;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.ComponentModel.DataAnnotations;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace ExtensionsDev
  17. {
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public static class SearchLookUpEditExtension
  22. {
  23. /// <summary>
  24. /// 设置SearchLookUpEdit默认属性
  25. /// </summary>
  26. /// <param name="ctrl"></param>
  27. /// <param name="nullText">数据源</param>
  28. /// <param name="showClearBtn"></param>
  29. public static SearchLookUpEdit UseDefault(this SearchLookUpEdit ctrl, string nullText = "请选择", bool showClearBtn = false)
  30. {
  31. ctrl.EditValue = null;
  32. ctrl.Properties.NullText = null;
  33. ctrl.Properties.NullValuePrompt = nullText;
  34. ctrl.Properties.AllowFocused = false;
  35. ctrl.Properties.ShowNullValuePrompt = ShowNullValuePromptOptions.Default;
  36. ctrl.Properties.ShowAddNewButton = false;
  37. ctrl.Properties.ShowClearButton = showClearBtn;
  38. ctrl.Properties.ShowFooter = false;
  39. ctrl.Properties.ShowPopupShadow = true;
  40. return ctrl;
  41. }
  42. /// <summary>
  43. /// 绑定SearchLookUpEdit数据源
  44. /// </summary>
  45. /// <param name="ctrl"></param>
  46. /// <param name="data">数据源</param>
  47. public static SearchLookUpEdit SetStringData(this SearchLookUpEdit ctrl, IEnumerable<string> data)
  48. {
  49. ctrl.Properties.DataSource = data;
  50. GridView view = ctrl.Properties.View;
  51. view.Tag = ctrl;
  52. view.OptionsView.ShowColumnHeaders = false;
  53. view.GridControl.UseDefault<string>(null);
  54. return ctrl;
  55. }
  56. /// <summary>
  57. /// 绑定SearchLookUpEdit数据源
  58. /// </summary>
  59. /// <param name="ctrl"></param>
  60. /// <param name="data">数据源</param>
  61. /// <param name="displayField"></param>
  62. public static SearchLookUpEdit SetData<T>(this SearchLookUpEdit ctrl, IEnumerable<T> data, string valueField = "Id", string displayField = "")
  63. {
  64. if (data == null) data = new List<T>();
  65. ctrl.Properties.ValueMember = valueField;
  66. ctrl.Properties.KeyMember = valueField;
  67. ctrl.Properties.DisplayMember = displayField;
  68. ctrl.QueryPopUp += Ctrl_QueryPopUp;
  69. ctrl.Properties.DataSource = data;
  70. GridView view = ctrl.Properties.View;
  71. view.Tag = ctrl;
  72. view.OptionsView.ShowColumnHeaders = true;
  73. view.GridControl.UseDefault<T>(null);
  74. ctrl.CustomDisplayText += (sender, e) =>
  75. {
  76. if (e.Value == null) return;
  77. if (typeof(T) != e.Value.GetType())
  78. {
  79. e.DisplayText = e.Value.ToString();
  80. }
  81. else if (string.IsNullOrWhiteSpace(displayField))
  82. {
  83. e.DisplayText = e.Value.ToString();
  84. }
  85. else
  86. {
  87. e.DisplayText = typeof(T).GetProperty(displayField).GetValue(e.Value).ToString();
  88. }
  89. };
  90. return ctrl;
  91. }
  92. private static async void Ctrl_QueryPopUp(object sender, System.ComponentModel.CancelEventArgs e)
  93. {
  94. var ctrl = sender as SearchLookUpEdit;
  95. var view = ctrl.Properties.View;
  96. view.GridControl.HideColumn("CreateTime", "UpdateTime", "Enable", "Enabled", "IsEnable", "Remark");
  97. view.GridControl.HideColumn(p => p.FieldName.ToUpper().EndsWith("ID"));
  98. view.GridControl.HideColumn(p => p.ColumnType == typeof(List<>));
  99. view.GridControl.HideColumn(p => p.ColumnType.BaseType != null && p.ColumnType.BaseType.Name.Contains("BaseEntity"));
  100. if (ctrl.Tag is RepositoryItemSearchLookUpEdit edit)
  101. {
  102. var onGetData = (Func<dynamic>)edit.Tag;
  103. if (onGetData != null)
  104. {
  105. ctrl.Properties.DataSource = await onGetData();
  106. edit.DataSource = await onGetData();
  107. }
  108. }
  109. else
  110. {
  111. var onGetData = (Func<dynamic>)ctrl.Tag;
  112. if (onGetData != null)
  113. {
  114. ctrl.Properties.DataSource = await onGetData();
  115. }
  116. }
  117. var dataType = view.DataSource.GetType().GetGenericArguments().Single();
  118. var props = dataType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  119. foreach (GridColumn column in view.Columns)
  120. {
  121. if (column.Visible)
  122. {
  123. var prop = props.FirstOrDefault(p => p.Name == column.FieldName);
  124. if (prop == null) continue;
  125. var attrDisplay = prop.GetCustomAttribute<DisplayAttribute>();
  126. var attrDisplayName = prop.GetCustomAttribute<DisplayNameAttribute>();
  127. if (attrDisplay == null && attrDisplayName == null)
  128. column.Visible = false;
  129. else if (attrDisplay != null && attrDisplay.GetAutoGenerateFilter() != null && !attrDisplay.AutoGenerateFilter)
  130. column.Visible = false;
  131. else
  132. column.Visible = true;
  133. }
  134. }
  135. }
  136. }
  137. }