DataGridRowGroupHeader.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 Avalonia.Controls.Metadata;
  6. using Avalonia.Controls.Mixins;
  7. using Avalonia.Controls.Primitives;
  8. using Avalonia.Input;
  9. using Avalonia.Media;
  10. using System;
  11. using System.Diagnostics;
  12. using System.Reactive.Linq;
  13. namespace Avalonia.Controls
  14. {
  15. [TemplatePart(DATAGRIDROWGROUPHEADER_expanderButton, typeof(ToggleButton))]
  16. [TemplatePart(DATAGRIDROWGROUPHEADER_indentSpacer, typeof(Control))]
  17. [TemplatePart(DATAGRIDROWGROUPHEADER_itemCountElement, typeof(TextBlock))]
  18. [TemplatePart(DATAGRIDROWGROUPHEADER_propertyNameElement, typeof(TextBlock))]
  19. [TemplatePart(DataGridRow.DATAGRIDROW_elementRoot, typeof(Panel))]
  20. [TemplatePart(DataGridRow.DATAGRIDROW_elementRowHeader, typeof(DataGridRowHeader))]
  21. [PseudoClasses(":pressed", ":current", ":expanded")]
  22. public class DataGridRowGroupHeader : TemplatedControl
  23. {
  24. private const string DATAGRIDROWGROUPHEADER_expanderButton = "ExpanderButton";
  25. private const string DATAGRIDROWGROUPHEADER_indentSpacer = "IndentSpacer";
  26. private const string DATAGRIDROWGROUPHEADER_itemCountElement = "ItemCountElement";
  27. private const string DATAGRIDROWGROUPHEADER_propertyNameElement = "PropertyNameElement";
  28. private bool _areIsCheckedHandlersSuspended;
  29. private ToggleButton _expanderButton;
  30. private DataGridRowHeader _headerElement;
  31. private Control _indentSpacer;
  32. private TextBlock _itemCountElement;
  33. private TextBlock _propertyNameElement;
  34. private Panel _rootElement;
  35. private double _totalIndent;
  36. public static readonly StyledProperty<bool> IsItemCountVisibleProperty =
  37. AvaloniaProperty.Register<DataGridRowGroupHeader, bool>(nameof(IsItemCountVisible));
  38. /// <summary>
  39. /// Gets or sets a value that indicates whether the item count is visible.
  40. /// </summary>
  41. public bool IsItemCountVisible
  42. {
  43. get { return GetValue(IsItemCountVisibleProperty); }
  44. set { SetValue(IsItemCountVisibleProperty, value); }
  45. }
  46. public static readonly StyledProperty<string> PropertyNameProperty =
  47. AvaloniaProperty.Register<DataGridRowGroupHeader, string>(nameof(PropertyName));
  48. /// <summary>
  49. /// Gets or sets the name of the property that this <see cref="T:Avalonia.Controls.DataGrid" /> row is bound to.
  50. /// </summary>
  51. public string PropertyName
  52. {
  53. get { return GetValue(PropertyNameProperty); }
  54. set { SetValue(PropertyNameProperty, value); }
  55. }
  56. public static readonly StyledProperty<bool> IsPropertyNameVisibleProperty =
  57. AvaloniaProperty.Register<DataGridRowGroupHeader, bool>(nameof(IsPropertyNameVisible));
  58. /// <summary>
  59. /// Gets or sets a value that indicates whether the property name is visible.
  60. /// </summary>
  61. public bool IsPropertyNameVisible
  62. {
  63. get { return GetValue(IsPropertyNameVisibleProperty); }
  64. set { SetValue(IsPropertyNameVisibleProperty, value); }
  65. }
  66. public static readonly StyledProperty<double> SublevelIndentProperty =
  67. AvaloniaProperty.Register<DataGridRowGroupHeader, double>(
  68. nameof(SublevelIndent),
  69. defaultValue: DataGrid.DATAGRID_defaultRowGroupSublevelIndent,
  70. validate: IsValidSublevelIndent);
  71. private static bool IsValidSublevelIndent(double value)
  72. {
  73. return !double.IsNaN(value) && !double.IsInfinity(value) && value >= 0;
  74. }
  75. /// <summary>
  76. /// Gets or sets a value that indicates the amount that the
  77. /// children of the <see cref="T:Avalonia.Controls.RowGroupHeader" /> are indented.
  78. /// </summary>
  79. public double SublevelIndent
  80. {
  81. get { return GetValue(SublevelIndentProperty); }
  82. set { SetValue(SublevelIndentProperty, value); }
  83. }
  84. private void OnSublevelIndentChanged(AvaloniaPropertyChangedEventArgs e)
  85. {
  86. if (OwningGrid != null)
  87. {
  88. OwningGrid.OnSublevelIndentUpdated(this, (double)e.NewValue);
  89. }
  90. }
  91. static DataGridRowGroupHeader()
  92. {
  93. SublevelIndentProperty.Changed.AddClassHandler<DataGridRowGroupHeader>((x,e) => x.OnSublevelIndentChanged(e));
  94. PressedMixin.Attach<DataGridRowGroupHeader>();
  95. }
  96. /// <summary>
  97. /// Constructs a DataGridRowGroupHeader
  98. /// </summary>
  99. public DataGridRowGroupHeader()
  100. {
  101. //DefaultStyleKey = typeof(DataGridRowGroupHeader);
  102. AddHandler(InputElement.PointerPressedEvent, (s, e) => DataGridRowGroupHeader_PointerPressed(e), handledEventsToo: true);
  103. }
  104. internal DataGridRowHeader HeaderCell
  105. {
  106. get
  107. {
  108. return _headerElement;
  109. }
  110. }
  111. private bool IsCurrent
  112. {
  113. get
  114. {
  115. Debug.Assert(OwningGrid != null);
  116. return (RowGroupInfo.Slot == OwningGrid.CurrentSlot);
  117. }
  118. }
  119. private bool IsMouseOver
  120. {
  121. get;
  122. set;
  123. }
  124. internal bool IsRecycled
  125. {
  126. get;
  127. set;
  128. }
  129. internal int Level
  130. {
  131. get;
  132. set;
  133. }
  134. internal DataGrid OwningGrid
  135. {
  136. get;
  137. set;
  138. }
  139. internal DataGridRowGroupInfo RowGroupInfo
  140. {
  141. get;
  142. set;
  143. }
  144. internal double TotalIndent
  145. {
  146. set
  147. {
  148. _totalIndent = value;
  149. if (_indentSpacer != null)
  150. {
  151. _indentSpacer.Width = _totalIndent;
  152. }
  153. }
  154. }
  155. private IDisposable _expanderButtonSubscription;
  156. protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
  157. {
  158. _rootElement = e.NameScope.Find<Panel>(DataGridRow.DATAGRIDROW_elementRoot);
  159. _expanderButtonSubscription?.Dispose();
  160. _expanderButton = e.NameScope.Find<ToggleButton>(DATAGRIDROWGROUPHEADER_expanderButton);
  161. if(_expanderButton != null)
  162. {
  163. EnsureExpanderButtonIsChecked();
  164. _expanderButtonSubscription =
  165. _expanderButton.GetObservable(ToggleButton.IsCheckedProperty)
  166. .Skip(1)
  167. .Subscribe(v => OnExpanderButtonIsCheckedChanged(v));
  168. }
  169. _headerElement = e.NameScope.Find<DataGridRowHeader>(DataGridRow.DATAGRIDROW_elementRowHeader);
  170. if(_headerElement != null)
  171. {
  172. _headerElement.Owner = this;
  173. EnsureHeaderVisibility();
  174. }
  175. _indentSpacer = e.NameScope.Find<Control>(DATAGRIDROWGROUPHEADER_indentSpacer);
  176. if(_indentSpacer != null)
  177. {
  178. _indentSpacer.Width = _totalIndent;
  179. }
  180. _itemCountElement = e.NameScope.Find<TextBlock>(DATAGRIDROWGROUPHEADER_itemCountElement);
  181. _propertyNameElement = e.NameScope.Find<TextBlock>(DATAGRIDROWGROUPHEADER_propertyNameElement);
  182. UpdateTitleElements();
  183. }
  184. internal void ApplyHeaderStatus()
  185. {
  186. if (_headerElement != null && OwningGrid.AreRowHeadersVisible)
  187. {
  188. _headerElement.UpdatePseudoClasses();
  189. }
  190. }
  191. internal void UpdatePseudoClasses()
  192. {
  193. PseudoClasses.Set(":current", IsCurrent);
  194. if (RowGroupInfo?.CollectionViewGroup != null)
  195. {
  196. PseudoClasses.Set(":expanded", RowGroupInfo.IsVisible && RowGroupInfo.CollectionViewGroup.ItemCount > 0);
  197. }
  198. }
  199. protected override Size ArrangeOverride(Size finalSize)
  200. {
  201. if (OwningGrid == null)
  202. {
  203. return base.ArrangeOverride(finalSize);
  204. }
  205. Size size = base.ArrangeOverride(finalSize);
  206. if (_rootElement != null)
  207. {
  208. if (OwningGrid.AreRowGroupHeadersFrozen)
  209. {
  210. foreach (Control child in _rootElement.Children)
  211. {
  212. child.Clip = null;
  213. }
  214. }
  215. else
  216. {
  217. double frozenLeftEdge = 0;
  218. foreach (Control child in _rootElement.Children)
  219. {
  220. if (DataGridFrozenGrid.GetIsFrozen(child) && child.IsVisible)
  221. {
  222. TranslateTransform transform = new TranslateTransform();
  223. // Automatic layout rounding doesn't apply to transforms so we need to Round this
  224. transform.X = Math.Round(OwningGrid.HorizontalOffset);
  225. child.RenderTransform = transform;
  226. double childLeftEdge = child.Translate(this, new Point(child.Bounds.Width, 0)).X - transform.X;
  227. frozenLeftEdge = Math.Max(frozenLeftEdge, childLeftEdge + OwningGrid.HorizontalOffset);
  228. }
  229. }
  230. // Clip the non-frozen elements so they don't overlap the frozen ones
  231. foreach (Control child in _rootElement.Children)
  232. {
  233. if (!DataGridFrozenGrid.GetIsFrozen(child))
  234. {
  235. EnsureChildClip(child, frozenLeftEdge);
  236. }
  237. }
  238. }
  239. }
  240. return size;
  241. }
  242. internal void ClearFrozenStates()
  243. {
  244. if (_rootElement != null)
  245. {
  246. foreach (Control child in _rootElement.Children)
  247. {
  248. child.RenderTransform = null;
  249. }
  250. }
  251. }
  252. //TODO TabStop
  253. private void DataGridRowGroupHeader_PointerPressed(PointerPressedEventArgs e)
  254. {
  255. if (OwningGrid == null)
  256. {
  257. return;
  258. }
  259. if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
  260. {
  261. if (OwningGrid.IsDoubleClickRecordsClickOnCall(this) && !e.Handled)
  262. {
  263. ToggleExpandCollapse(!RowGroupInfo.IsVisible, true);
  264. e.Handled = true;
  265. }
  266. else
  267. {
  268. //if (!e.Handled && OwningGrid.IsTabStop)
  269. if (!e.Handled)
  270. {
  271. OwningGrid.Focus();
  272. }
  273. e.Handled = OwningGrid.UpdateStateOnMouseLeftButtonDown(e, OwningGrid.CurrentColumnIndex, RowGroupInfo.Slot, allowEdit: false);
  274. }
  275. }
  276. else if (e.GetCurrentPoint(this).Properties.IsRightButtonPressed)
  277. {
  278. if (!e.Handled)
  279. {
  280. OwningGrid.Focus();
  281. }
  282. e.Handled = OwningGrid.UpdateStateOnMouseRightButtonDown(e, OwningGrid.CurrentColumnIndex, RowGroupInfo.Slot, allowEdit: false);
  283. }
  284. }
  285. private void EnsureChildClip(Visual child, double frozenLeftEdge)
  286. {
  287. double childLeftEdge = child.Translate(this, new Point(0, 0)).X;
  288. if (frozenLeftEdge > childLeftEdge)
  289. {
  290. double xClip = Math.Round(frozenLeftEdge - childLeftEdge);
  291. var rg = new RectangleGeometry();
  292. rg.Rect =
  293. new Rect(xClip, 0,
  294. Math.Max(0, child.Bounds.Width - xClip),
  295. child.Bounds.Height);
  296. child.Clip = rg;
  297. }
  298. else
  299. {
  300. child.Clip = null;
  301. }
  302. }
  303. internal void EnsureExpanderButtonIsChecked()
  304. {
  305. if (_expanderButton != null && RowGroupInfo != null && RowGroupInfo.CollectionViewGroup != null &&
  306. RowGroupInfo.CollectionViewGroup.ItemCount != 0)
  307. {
  308. SetIsCheckedNoCallBack(RowGroupInfo.IsVisible);
  309. }
  310. }
  311. //TODO Styles
  312. //internal void EnsureHeaderStyleAndVisibility(Style previousStyle)
  313. internal void EnsureHeaderVisibility()
  314. {
  315. if (_headerElement != null && OwningGrid != null)
  316. {
  317. _headerElement.IsVisible = OwningGrid.AreRowHeadersVisible;
  318. }
  319. }
  320. private void OnExpanderButtonIsCheckedChanged(bool? value)
  321. {
  322. if(!_areIsCheckedHandlersSuspended)
  323. {
  324. ToggleExpandCollapse(value ?? false, true);
  325. }
  326. }
  327. internal void LoadVisualsForDisplay()
  328. {
  329. EnsureExpanderButtonIsChecked();
  330. EnsureHeaderVisibility();
  331. UpdatePseudoClasses();
  332. ApplyHeaderStatus();
  333. }
  334. protected override void OnPointerEntered(PointerEventArgs e)
  335. {
  336. if (IsEnabled)
  337. {
  338. IsMouseOver = true;
  339. UpdatePseudoClasses();
  340. }
  341. base.OnPointerEntered(e);
  342. }
  343. protected override void OnPointerExited(PointerEventArgs e)
  344. {
  345. if (IsEnabled)
  346. {
  347. IsMouseOver = false;
  348. UpdatePseudoClasses();
  349. }
  350. base.OnPointerExited(e);
  351. }
  352. private void SetIsCheckedNoCallBack(bool value)
  353. {
  354. if (_expanderButton != null && _expanderButton.IsChecked != value)
  355. {
  356. _areIsCheckedHandlersSuspended = true;
  357. try
  358. {
  359. _expanderButton.IsChecked = value;
  360. }
  361. finally
  362. {
  363. _areIsCheckedHandlersSuspended = false;
  364. }
  365. }
  366. }
  367. internal void ToggleExpandCollapse(bool isVisible, bool setCurrent)
  368. {
  369. if (RowGroupInfo.CollectionViewGroup.ItemCount != 0)
  370. {
  371. if (OwningGrid == null)
  372. {
  373. // Do these even if the OwningGrid is null in case it could improve the Designer experience for a standalone DataGridRowGroupHeader
  374. RowGroupInfo.IsVisible = isVisible;
  375. }
  376. else if(RowGroupInfo.IsVisible != isVisible)
  377. {
  378. OwningGrid.OnRowGroupHeaderToggled(this, isVisible, setCurrent);
  379. }
  380. EnsureExpanderButtonIsChecked();
  381. UpdatePseudoClasses();
  382. }
  383. }
  384. internal void UpdateTitleElements()
  385. {
  386. if (_propertyNameElement != null)
  387. {
  388. string txt;
  389. if (string.IsNullOrWhiteSpace(PropertyName))
  390. txt = String.Empty;
  391. else
  392. txt = String.Format("{0}:", PropertyName);
  393. _propertyNameElement.Text = txt;
  394. }
  395. if (_itemCountElement != null && RowGroupInfo != null && RowGroupInfo.CollectionViewGroup != null)
  396. {
  397. string formatString;
  398. if(RowGroupInfo.CollectionViewGroup.ItemCount == 1)
  399. formatString = "({0} Item)";
  400. else
  401. formatString = "({0} Items)";
  402. _itemCountElement.Text = String.Format(formatString, RowGroupInfo.CollectionViewGroup.ItemCount);
  403. }
  404. }
  405. }
  406. }