DataGridClipboard.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // (c) Copyright Microsoft Corporation.
  2. // This source is subject to the Microsoft Public License (Ms-PL).
  3. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  4. // All other rights reserved.
  5. using System;
  6. using System.Collections.Generic;
  7. namespace Avalonia.Controls
  8. {
  9. /// <summary>
  10. /// Defines modes that indicates how DataGrid content is copied to the Clipboard.
  11. /// </summary>
  12. public enum DataGridClipboardCopyMode
  13. {
  14. /// <summary>
  15. /// Disable the DataGrid's ability to copy selected items as text.
  16. /// </summary>
  17. None,
  18. /// <summary>
  19. /// Enable the DataGrid's ability to copy selected items as text, but do not include
  20. /// the column header content as the first line in the text that gets copied to the Clipboard.
  21. /// </summary>
  22. ExcludeHeader,
  23. /// <summary>
  24. /// Enable the DataGrid's ability to copy selected items as text, and include
  25. /// the column header content as the first line in the text that gets copied to the Clipboard.
  26. /// </summary>
  27. IncludeHeader
  28. }
  29. /// <summary>
  30. /// This structure encapsulate the cell information necessary when clipboard content is prepared.
  31. /// </summary>
  32. public struct DataGridClipboardCellContent
  33. {
  34. private DataGridColumn _column;
  35. private object _content;
  36. private object _item;
  37. /// <summary>
  38. /// Creates a new DataGridClipboardCellValue structure containing information about a DataGrid cell.
  39. /// </summary>
  40. /// <param name="item">DataGrid row item containing the cell.</param>
  41. /// <param name="column">DataGridColumn containing the cell.</param>
  42. /// <param name="content">DataGrid cell value.</param>
  43. public DataGridClipboardCellContent(object item, DataGridColumn column, object content)
  44. {
  45. this._item = item;
  46. this._column = column;
  47. this._content = content;
  48. }
  49. /// <summary>
  50. /// DataGridColumn containing the cell.
  51. /// </summary>
  52. public DataGridColumn Column
  53. {
  54. get
  55. {
  56. return _column;
  57. }
  58. }
  59. /// <summary>
  60. /// Cell content.
  61. /// </summary>
  62. public object Content
  63. {
  64. get
  65. {
  66. return _content;
  67. }
  68. }
  69. /// <summary>
  70. /// DataGrid row item containing the cell.
  71. /// </summary>
  72. public object Item
  73. {
  74. get
  75. {
  76. return _item;
  77. }
  78. }
  79. /// <summary>
  80. /// Field-by-field comparison to avoid reflection-based ValueType.Equals.
  81. /// </summary>
  82. /// <param name="obj">DataGridClipboardCellContent to compare.</param>
  83. /// <returns>True iff this and data are equal</returns>
  84. public override bool Equals(object obj)
  85. {
  86. if(obj is DataGridClipboardCellContent content)
  87. {
  88. return (((_column == content._column) && (_content == content._content)) && (_item == content._item));
  89. }
  90. else
  91. {
  92. return false;
  93. }
  94. }
  95. /// <summary>
  96. /// Returns a deterministic hash code.
  97. /// </summary>
  98. /// <returns>Hash value.</returns>
  99. public override int GetHashCode()
  100. {
  101. return ((_column.GetHashCode() ^ _content.GetHashCode()) ^ _item.GetHashCode());
  102. }
  103. /// <summary>
  104. /// Field-by-field comparison to avoid reflection-based ValueType.Equals.
  105. /// </summary>
  106. /// <param name="clipboardCellContent1">The first DataGridClipboardCellContent.</param>
  107. /// <param name="clipboardCellContent2">The second DataGridClipboardCellContent.</param>
  108. /// <returns>True iff clipboardCellContent1 and clipboardCellContent2 are equal.</returns>
  109. public static bool operator ==(DataGridClipboardCellContent clipboardCellContent1, DataGridClipboardCellContent clipboardCellContent2)
  110. {
  111. return (((clipboardCellContent1._column == clipboardCellContent2._column) && (clipboardCellContent1._content == clipboardCellContent2._content)) && (clipboardCellContent1._item == clipboardCellContent2._item));
  112. }
  113. /// <summary>
  114. /// Field-by-field comparison to avoid reflection-based ValueType.Equals.
  115. /// </summary>
  116. /// <param name="clipboardCellContent1">The first DataGridClipboardCellContent.</param>
  117. /// <param name="clipboardCellContent2">The second DataGridClipboardCellContent.</param>
  118. /// <returns>True iff clipboardCellContent1 and clipboardCellContent2 are NOT equal.</returns>
  119. public static bool operator !=(DataGridClipboardCellContent clipboardCellContent1, DataGridClipboardCellContent clipboardCellContent2)
  120. {
  121. if ((clipboardCellContent1._column == clipboardCellContent2._column) && (clipboardCellContent1._content == clipboardCellContent2._content))
  122. {
  123. return (clipboardCellContent1._item != clipboardCellContent2._item);
  124. }
  125. return true;
  126. }
  127. }
  128. /// <summary>
  129. /// This class encapsulates a selected row's information necessary for the CopyingRowClipboardContent event.
  130. /// </summary>
  131. public class DataGridRowClipboardEventArgs : EventArgs
  132. {
  133. private List<DataGridClipboardCellContent> _clipboardRowContent;
  134. private bool _isColumnHeadersRow;
  135. private object _item;
  136. /// <summary>
  137. /// Creates a DataGridRowClipboardEventArgs object and initializes the properties.
  138. /// </summary>
  139. /// <param name="item">The row's associated data item.</param>
  140. /// <param name="isColumnHeadersRow">Whether or not this EventArgs is for the column headers.</param>
  141. internal DataGridRowClipboardEventArgs(object item, bool isColumnHeadersRow)
  142. {
  143. _isColumnHeadersRow = isColumnHeadersRow;
  144. _item = item;
  145. }
  146. /// <summary>
  147. /// This list should be used to modify, add ot remove a cell content before it gets stored into the clipboard.
  148. /// </summary>
  149. public List<DataGridClipboardCellContent> ClipboardRowContent
  150. {
  151. get
  152. {
  153. if (_clipboardRowContent == null)
  154. {
  155. _clipboardRowContent = new List<DataGridClipboardCellContent>();
  156. }
  157. return _clipboardRowContent;
  158. }
  159. }
  160. /// <summary>
  161. /// This property is true when the ClipboardRowContent represents column headers, in which case the Item is null.
  162. /// </summary>
  163. public bool IsColumnHeadersRow
  164. {
  165. get
  166. {
  167. return _isColumnHeadersRow;
  168. }
  169. }
  170. /// <summary>
  171. /// DataGrid row item used for proparing the ClipboardRowContent.
  172. /// </summary>
  173. public object Item
  174. {
  175. get
  176. {
  177. return _item;
  178. }
  179. }
  180. }
  181. }