AvaloniaDataGrid.axaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using Avalonia;
  2. using Avalonia.Collections;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Input;
  6. using Avalonia.Markup.Xaml;
  7. using Avalonia.Media;
  8. using Avalonia.VisualTree;
  9. using CommunityToolkit.Mvvm.Input;
  10. using InABox.Core;
  11. using System.Collections;
  12. using System.ComponentModel;
  13. namespace InABox.Avalonia.Components;
  14. public class AvaloniaDataGridSelectionChangedEventArgs(object?[] selection)
  15. {
  16. public object?[] Selection { get; set; } = selection;
  17. }
  18. public class AvaloniaDataGridRefreshRequestedEventArgs()
  19. {
  20. }
  21. public partial class AvaloniaDataGrid : UserControl, INotifyPropertyChanged
  22. {
  23. public static StyledProperty<bool> CanSearchProperty =
  24. AvaloniaProperty.Register<AvaloniaDataGrid, bool>(nameof(CanSearch), true);
  25. public static StyledProperty<IEnumerable> ItemsSourceProperty =
  26. AvaloniaProperty.Register<AvaloniaDataGrid, IEnumerable>(nameof(ItemsSource));
  27. public static StyledProperty<DateTime> LastUpdatedProperty =
  28. AvaloniaProperty.Register<AvaloniaDataGrid, DateTime>(nameof(LastUpdated));
  29. public static StyledProperty<bool> ShowRecordCountProperty =
  30. AvaloniaProperty.Register<AvaloniaDataGrid, bool>(nameof(ShowRecordCount));
  31. public static StyledProperty<bool> RefreshVisibleProperty =
  32. AvaloniaProperty.Register<AvaloniaDataGrid, bool>(nameof(RefreshVisible));
  33. public static StyledProperty<SelectionMode> SelectionModeProperty =
  34. AvaloniaProperty.Register<AvaloniaDataGrid, SelectionMode>(nameof(SelectionMode), SelectionMode.Single);
  35. public static StyledProperty<double> RowHeightProperty =
  36. AvaloniaProperty.Register<AvaloniaDataGrid, double>(nameof(RowHeight), 30);
  37. public static StyledProperty<AvaloniaDataGridColumns?> ColumnsProperty =
  38. AvaloniaProperty.Register<AvaloniaDataGrid, AvaloniaDataGridColumns?>(nameof(Columns), null);
  39. public string SearchText { get; set; } = "";
  40. public bool CanSearch
  41. {
  42. get => GetValue(CanSearchProperty);
  43. set => SetValue(CanSearchProperty, value);
  44. }
  45. public IEnumerable ItemsSource
  46. {
  47. get => GetValue(ItemsSourceProperty);
  48. set => SetValue(ItemsSourceProperty, value);
  49. }
  50. public bool ShowRecordCount
  51. {
  52. get => GetValue(ShowRecordCountProperty);
  53. set => SetValue(ShowRecordCountProperty, value);
  54. }
  55. public bool RefreshVisible
  56. {
  57. get => GetValue(RefreshVisibleProperty);
  58. set => SetValue(RefreshVisibleProperty, value);
  59. }
  60. public SelectionMode SelectionMode
  61. {
  62. get => GetValue(SelectionModeProperty);
  63. set => SetValue(SelectionModeProperty, value);
  64. }
  65. public double RowHeight
  66. {
  67. get => GetValue(RowHeightProperty);
  68. set => SetValue(RowHeightProperty, value);
  69. }
  70. public int ItemCount { get; set; }
  71. public DateTime LastUpdated
  72. {
  73. get => GetValue(LastUpdatedProperty);
  74. set => SetValue(LastUpdatedProperty, value);
  75. }
  76. public AvaloniaDataGridColumns Columns { get; private set; }
  77. public IEnumerable<object?> SelectedItems => Grid.SelectedItems.Cast<object?>();
  78. public event EventHandler<AvaloniaDataGridSelectionChangedEventArgs>? SelectionChanged;
  79. public event EventHandler<AvaloniaDataGridRefreshRequestedEventArgs>? RefreshRequested;
  80. #region Static Constructor and Property Changed Handlers
  81. static AvaloniaDataGrid()
  82. {
  83. ItemsSourceProperty.Changed.AddClassHandler<AvaloniaDataGrid>(ItemsSource_Changed);
  84. LastUpdatedProperty.Changed.AddClassHandler<AvaloniaDataGrid>(LastUpdated_Changed);
  85. ShowRecordCountProperty.Changed.AddClassHandler<AvaloniaDataGrid>(ShowRecordCount_Changed);
  86. ColumnsProperty.Changed.AddClassHandler<AvaloniaDataGrid>(ColumnsProperty_Changed);
  87. }
  88. private static void ColumnsProperty_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  89. {
  90. var columns = grid.GetValue(ColumnsProperty);
  91. if(columns is not null)
  92. {
  93. grid.Columns.BeginUpdate().AddRange(columns).EndUpdate();
  94. }
  95. }
  96. private static void ShowRecordCount_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  97. {
  98. grid.UpdateSummaryRow();
  99. }
  100. private static void LastUpdated_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  101. {
  102. grid.UpdateSummaryRow();
  103. }
  104. private static void ItemsSource_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  105. {
  106. grid.Grid.ItemsSource = grid.ItemsSource;
  107. if (grid.Grid.CollectionView is not null)
  108. {
  109. grid.Grid.CollectionView.CollectionChanged -= grid.CollectionView_CollectionChanged;
  110. grid.Grid.CollectionView.CollectionChanged += grid.CollectionView_CollectionChanged;
  111. }
  112. grid.UpdateSummaryRow();
  113. }
  114. #endregion
  115. public AvaloniaDataGrid()
  116. {
  117. InitializeComponent();
  118. Columns = new AvaloniaDataGridColumns();
  119. Columns.Changed += Columns_Changed;
  120. }
  121. private void CollectionView_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  122. {
  123. ItemCount = (Grid.CollectionView as DataGridCollectionView)?.ItemCount ?? 0;
  124. }
  125. private void Columns_Changed(AvaloniaDataGridColumns columns)
  126. {
  127. Grid.Columns.Clear();
  128. foreach(var column in columns)
  129. {
  130. Grid.Columns.Add(column.CreateColumn());
  131. }
  132. // Summaries
  133. var searchableColumns = Columns.Any(x => x.Searchable);
  134. SearchBar.IsVisible = searchableColumns && CanSearch;
  135. }
  136. private void UpdateSummaryRow()
  137. {
  138. _lastUpdated.IsVisible = LastUpdated != DateTime.MinValue;
  139. _recordCount.Content = $" {ItemCount} records";
  140. _recordCount.IsVisible = ShowRecordCount && ItemsSource is IEnumerable;
  141. _recordCountBox.IsVisible = _recordCount.IsVisible || _lastUpdated.IsVisible;
  142. }
  143. public void ClearSelection()
  144. {
  145. Grid.SelectedItem = null;
  146. Grid.SelectedItems.Clear();
  147. }
  148. private bool DoSearch(object? item)
  149. {
  150. if (SearchText.IsNullOrWhiteSpace()) return true;
  151. if (item is null) return false;
  152. foreach(var column in Columns)
  153. {
  154. if(column.Filter(item, SearchText)) return true;
  155. }
  156. return false;
  157. }
  158. [RelayCommand]
  159. private void Search()
  160. {
  161. if (Grid.CollectionView is null) return;
  162. Grid.CollectionView.Filter = DoSearch;
  163. Grid.CollectionView.Refresh();
  164. UpdateSummaryRow();
  165. }
  166. [RelayCommand]
  167. private void Refresh()
  168. {
  169. if (Grid.CollectionView is null) return;
  170. RefreshRequested?.Invoke(this, new());
  171. Grid.CollectionView.Refresh();
  172. }
  173. private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  174. {
  175. if (SelectionMode != SelectionMode.Multiple) return;
  176. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(SelectedItems.ToArray()));
  177. }
  178. private void DataGrid_Tapped(object sender, TappedEventArgs e)
  179. {
  180. if (SelectionMode == SelectionMode.Multiple) return;
  181. var position = e.GetPosition(Grid);
  182. var parent = (e.Source as Visual)?.GetVisualAncestors().Where(x => x is DataGridCell || x is DataGridColumnHeader).FirstOrDefault();
  183. if (parent is null) return;
  184. if(parent is DataGridCell cell)
  185. {
  186. var cellCollection = cell.GetVisualParent<DataGridCellsPresenter>();
  187. if (cellCollection is null) return;
  188. var colIdx = cellCollection.Children.IndexOf(cell);
  189. var row = cellCollection.GetVisualAncestors().OfType<DataGridRow>().FirstOrDefault();
  190. if (row is null) return;
  191. var rowCollection = row.GetVisualParent<DataGridRowsPresenter>();
  192. if (rowCollection is null) return;
  193. var rowIdx = row.Index;
  194. var item = (Grid.CollectionView as DataGridCollectionView)?.GetItemAt(rowIdx);
  195. var column = Columns[colIdx];
  196. if(column.Tapped is not null)
  197. {
  198. column.Tapped?.Invoke(column, item);
  199. }
  200. else
  201. {
  202. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(new object?[] { item }));
  203. }
  204. }
  205. else if(parent is DataGridColumnHeader header)
  206. {
  207. var headerCollection = header.GetVisualParent<DataGridColumnHeadersPresenter>();
  208. if (headerCollection is null) return;
  209. var colIdx = headerCollection.Children.IndexOf(header);
  210. var column = Columns[colIdx];
  211. if(column.Tapped is not null)
  212. {
  213. column.Tapped?.Invoke(column, null);
  214. }
  215. else
  216. {
  217. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(Array.Empty<object?>()));
  218. }
  219. }
  220. }
  221. }