/Coevery.Win.Common/Controls/GridLookUpExtention.cs

http://coevery.codeplex.com · C# · 237 lines · 175 code · 30 blank · 32 comment · 8 complexity · 5d9fe4905c53d4fb245da38c6aa7864e MD5 · raw file

  1. // Developer Express Code Central Example:
  2. // GridLookUpEdit: multiple selection using a checkbox (web style) with the predefined OK and Cancel buttons
  3. //
  4. // The current example extends the functionality described in
  5. // http://www.devexpress.com/scid=E3074 example.
  6. // In the mentioned example,
  7. // GridlookUpEdit.EditValue is changed immediately after a user has selected a row
  8. // in a drop-down GridView instance.
  9. // This example demonstrates how to implement
  10. // the functionality to confirm/reject changing GridlookUpEdit.EditValue when a
  11. // popup Form is closing. This functionality is implemented by adding two
  12. // predefined buttons: Ok and Cancel.
  13. //
  14. // In addition, this example illustrates how
  15. // to use a different collection as a GridlookUpEdit.DataSource property value.
  16. //
  17. // You can find sample updates and versions for different programming languages here:
  18. // http://www.devexpress.com/example=E4048
  19. using System;
  20. using System.Drawing;
  21. using System.Text;
  22. using DevExpress.XtraEditors.Repository;
  23. using DevExpress.XtraEditors;
  24. using DevExpress.XtraEditors.Registrator;
  25. using DevExpress.XtraEditors.ViewInfo;
  26. using DevExpress.XtraEditors.Drawing;
  27. using System.ComponentModel;
  28. using DevExpress.XtraEditors.Popup;
  29. using System.Collections;
  30. using System.Windows.Forms;
  31. namespace Coevery.Win.Common.Controls
  32. {
  33. [UserRepositoryItem("RegisterMyRepositoryItemGridLookUpEdit")]
  34. public class MyRepositoryItemGridLookUpEdit : RepositoryItemGridLookUpEdit
  35. {
  36. //The static constructor which calls the registration method
  37. static MyRepositoryItemGridLookUpEdit() { RegisterMyRepositoryItemGridLookUpEdit(); }
  38. internal GridCheckMarksSelection gridSelection_;
  39. public GridCheckMarksSelection GridSelection
  40. {
  41. get { return gridSelection_; }
  42. set
  43. {
  44. if (gridSelection_ != null)
  45. {
  46. gridSelection_.SelectionChanged -= new GridCheckMarksSelection.SelectionChangedEventHandler(gridSelection__SelectionChanged);
  47. }
  48. gridSelection_ = value;
  49. gridSelection_.SelectionChanged += new GridCheckMarksSelection.SelectionChangedEventHandler(gridSelection__SelectionChanged);
  50. }
  51. }
  52. void gridSelection__SelectionChanged(object sender, EventArgs e)
  53. {
  54. StringBuilder sb = new StringBuilder();
  55. PropertyDescriptorCollection collection = ListBindingHelper.GetListItemProperties(GridSelection.Selection);
  56. PropertyDescriptor desc = collection[DisplayMember];
  57. foreach (object rv in GridSelection.Selection)
  58. {
  59. if (sb.ToString().Length > 0) { sb.Append(", "); }
  60. sb.Append(desc.GetValue(rv).ToString());
  61. }
  62. if (OwnerEdit != null)
  63. {
  64. OwnerEdit.Text = sb.ToString();
  65. }
  66. }
  67. //Initialize new properties
  68. public MyRepositoryItemGridLookUpEdit()
  69. {
  70. GridSelection = new GridCheckMarksSelection();
  71. }
  72. //The unique name for the custom editor
  73. public const string CustomEditName = "MyGridLookUpEdit";
  74. //Return the unique name
  75. public override string EditorTypeName { get { return CustomEditName; } }
  76. //Register the editor
  77. public static void RegisterMyRepositoryItemGridLookUpEdit()
  78. {
  79. EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(
  80. CustomEditName,
  81. typeof(MyGridLookUpEdit),
  82. typeof(MyRepositoryItemGridLookUpEdit),
  83. typeof(GridLookUpEditBaseViewInfo),
  84. new ButtonEditPainter(), true, null));
  85. }
  86. public override void Assign(RepositoryItem item)
  87. {
  88. BeginUpdate();
  89. try
  90. {
  91. base.Assign(item);
  92. MyRepositoryItemGridLookUpEdit source = item as MyRepositoryItemGridLookUpEdit;
  93. if (source == null) return;
  94. GridSelection = source.GridSelection;
  95. }
  96. finally
  97. {
  98. EndUpdate();
  99. }
  100. }
  101. }
  102. public class MyGridLookUpEdit : GridLookUpEdit
  103. {
  104. //The static constructor which calls the registration method
  105. static MyGridLookUpEdit() { MyRepositoryItemGridLookUpEdit.RegisterMyRepositoryItemGridLookUpEdit(); }
  106. //Initialize the new instance
  107. public MyGridLookUpEdit()
  108. {
  109. //...
  110. CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(MyGridLookUpEdit_CustomDisplayText);
  111. QueryPopUp += new CancelEventHandler(MyGridLookUpEdit_QueryPopUp);
  112. }
  113. void MyGridLookUpEdit_QueryPopUp(object sender, CancelEventArgs e)
  114. {
  115. GridLookUpEdit editor = (GridLookUpEdit)sender;
  116. RepositoryItemGridLookUpEdit properties = editor.Properties;
  117. properties.PopupFormSize = new Size(editor.Width - 4, properties.PopupFormSize.Height);
  118. }
  119. void MyGridLookUpEdit_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
  120. {
  121. StringBuilder sb = new StringBuilder();
  122. PropertyDescriptorCollection collection = ListBindingHelper.GetListItemProperties(Properties.GridSelection.Selection);
  123. PropertyDescriptor desc = collection[Properties.DisplayMember];
  124. foreach (object rv in Properties.GridSelection.Selection)
  125. {
  126. if (sb.ToString().Length > 0) { sb.Append(", "); }
  127. sb.Append(desc.GetValue(rv).ToString());
  128. }
  129. e.DisplayText = sb.ToString();
  130. }
  131. //Return the unique name
  132. public override string EditorTypeName
  133. {
  134. get
  135. {
  136. return MyRepositoryItemGridLookUpEdit.CustomEditName;
  137. }
  138. }
  139. //Override the Properties property
  140. //Simply type-cast the object to the custom repository item type
  141. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  142. public new MyRepositoryItemGridLookUpEdit Properties
  143. {
  144. get { return base.Properties as MyRepositoryItemGridLookUpEdit; }
  145. }
  146. protected override DevExpress.XtraEditors.Popup.PopupBaseForm CreatePopupForm()
  147. {
  148. return new MyPopupGridLookUpEditForm(this);
  149. }
  150. protected override void OnPopupShown()
  151. {
  152. base.OnPopupShown();
  153. }
  154. }
  155. public class MyPopupGridLookUpEditForm : PopupGridLookUpEditForm
  156. {
  157. public MyPopupGridLookUpEditForm(GridLookUpEdit edit) : base(edit) { }
  158. ArrayList tempSelection = new ArrayList();
  159. protected override void SetupButtons()
  160. {
  161. this.fShowOkButton = true;
  162. this.fCloseButtonStyle = BlobCloseButtonStyle.Caption;
  163. }
  164. protected override void OnCloseButtonClick()
  165. {
  166. base.OnCloseButtonClick();
  167. RedefineSelection(tempSelection, (OwnerEdit as MyGridLookUpEdit).Properties.GridSelection.Selection);
  168. }
  169. protected override void OnOkButtonClick()
  170. {
  171. base.OnOkButtonClick();
  172. (OwnerEdit as MyGridLookUpEdit).Properties.GridSelection.OnSelectionChanged();
  173. }
  174. protected override void OnBeforeShowPopup()
  175. {
  176. base.OnBeforeShowPopup();
  177. RedefineSelection((OwnerEdit as MyGridLookUpEdit).Properties.GridSelection.Selection, tempSelection);
  178. }
  179. internal void RedefineSelection(ArrayList listSource, ArrayList listDestination)
  180. {
  181. listDestination.Clear();
  182. foreach (var item in listSource)
  183. listDestination.Add(item);
  184. }
  185. protected override PopupBaseFormViewInfo CreateViewInfo()
  186. {
  187. return new MyCustomBlobPopupFormViewInfo(this);
  188. }
  189. }
  190. public class MyCustomBlobPopupFormViewInfo : CustomBlobPopupFormViewInfo
  191. {
  192. public MyCustomBlobPopupFormViewInfo(PopupBaseForm form) : base(form) { }
  193. // recalculate buttons sizes if needed
  194. protected override void CalcContentRect(System.Drawing.Rectangle bounds)
  195. {
  196. base.CalcContentRect(bounds);
  197. // recalculate buttons bounds if needed
  198. this.fOkButtonRect = new System.Drawing.Rectangle(this.fOkButtonRect.X, this.fOkButtonRect.Y + 1, this.fOkButtonRect.Width, this.fOkButtonRect.Height - 2);
  199. this.fCloseButtonRect = new System.Drawing.Rectangle(this.fCloseButtonRect.X, this.fCloseButtonRect.Y + 1, this.fCloseButtonRect.Width, this.fCloseButtonRect.Height - 2);
  200. // recalculate footer bounds if needed
  201. this.SizeBarRect = new System.Drawing.Rectangle(this.SizeBarRect.X, this.SizeBarRect.Y - 20, this.SizeBarRect.Width, this.SizeBarRect.Height + 20);
  202. this.fContentRect = new System.Drawing.Rectangle(this.fContentRect.X, this.fContentRect.Y, this.fContentRect.Width, this.fContentRect.Height - 20);
  203. this.fOkButtonRect = new System.Drawing.Rectangle(this.fOkButtonRect.X, this.fOkButtonRect.Y - 10, this.fOkButtonRect.Width, this.fOkButtonRect.Height);
  204. this.fCloseButtonRect = new System.Drawing.Rectangle(this.fCloseButtonRect.X, this.fCloseButtonRect.Y - 10, this.fCloseButtonRect.Width, this.fCloseButtonRect.Height);
  205. }
  206. }
  207. }