DataGridRowGroupHeader.cs 16 KB

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