AvaloniaDataGrid.axaml.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.ItemsChanged();
  113. grid.UpdateSummaryRow();
  114. }
  115. #endregion
  116. public AvaloniaDataGrid()
  117. {
  118. InitializeComponent();
  119. Columns = new AvaloniaDataGridColumns();
  120. Columns.Changed += Columns_Changed;
  121. }
  122. private void CollectionView_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  123. {
  124. ItemsChanged();
  125. }
  126. private void ItemsChanged()
  127. {
  128. ItemCount = (Grid.CollectionView as DataGridCollectionView)?.ItemCount ?? 0;
  129. }
  130. private void Columns_Changed(AvaloniaDataGridColumns columns)
  131. {
  132. Grid.Columns.Clear();
  133. foreach(var column in columns)
  134. {
  135. Grid.Columns.Add(column.CreateColumn());
  136. }
  137. // Summaries
  138. var searchableColumns = Columns.Any(x => x.Searchable);
  139. SearchBar.IsVisible = searchableColumns && CanSearch;
  140. }
  141. private void UpdateSummaryRow()
  142. {
  143. _lastUpdated.IsVisible = LastUpdated != DateTime.MinValue;
  144. _recordCount.Content = $" {ItemCount} records";
  145. _recordCount.IsVisible = ShowRecordCount && ItemsSource is IEnumerable;
  146. _recordCountBox.IsVisible = _recordCount.IsVisible || _lastUpdated.IsVisible;
  147. }
  148. public void ClearSelection()
  149. {
  150. Grid.SelectedItem = null;
  151. Grid.SelectedItems.Clear();
  152. }
  153. private bool DoSearch(object? item)
  154. {
  155. if (SearchText.IsNullOrWhiteSpace()) return true;
  156. if (item is null) return false;
  157. foreach(var column in Columns)
  158. {
  159. if(column.Filter(item, SearchText)) return true;
  160. }
  161. return false;
  162. }
  163. [RelayCommand]
  164. private void Search()
  165. {
  166. if (Grid.CollectionView is null) return;
  167. Grid.CollectionView.Filter = DoSearch;
  168. Grid.CollectionView.Refresh();
  169. UpdateSummaryRow();
  170. }
  171. [RelayCommand]
  172. private void Refresh()
  173. {
  174. if (Grid.CollectionView is null) return;
  175. RefreshRequested?.Invoke(this, new());
  176. Grid.CollectionView.Refresh();
  177. }
  178. private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  179. {
  180. if (SelectionMode != SelectionMode.Multiple) return;
  181. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(SelectedItems.ToArray()));
  182. }
  183. private void DataGrid_Tapped(object sender, TappedEventArgs e)
  184. {
  185. if (SelectionMode == SelectionMode.Multiple) return;
  186. var position = e.GetPosition(Grid);
  187. var parent = (e.Source as Visual)?.GetVisualAncestors().Where(x => x is DataGridCell || x is DataGridColumnHeader).FirstOrDefault();
  188. if (parent is null) return;
  189. if(parent is DataGridCell cell)
  190. {
  191. var cellCollection = cell.GetVisualParent<DataGridCellsPresenter>();
  192. if (cellCollection is null) return;
  193. var colIdx = cellCollection.Children.IndexOf(cell);
  194. var row = cellCollection.GetVisualAncestors().OfType<DataGridRow>().FirstOrDefault();
  195. if (row is null) return;
  196. var rowCollection = row.GetVisualParent<DataGridRowsPresenter>();
  197. if (rowCollection is null) return;
  198. var rowIdx = row.Index;
  199. var item = (Grid.CollectionView as DataGridCollectionView)?.GetItemAt(rowIdx);
  200. var column = Columns[colIdx];
  201. if(column.Tapped is not null)
  202. {
  203. column.Tapped?.Invoke(column, item);
  204. }
  205. else
  206. {
  207. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(new object?[] { item }));
  208. }
  209. }
  210. else if(parent is DataGridColumnHeader header)
  211. {
  212. var headerCollection = header.GetVisualParent<DataGridColumnHeadersPresenter>();
  213. if (headerCollection is null) return;
  214. var colIdx = headerCollection.Children.IndexOf(header);
  215. var column = Columns[colIdx];
  216. if(column.Tapped is not null)
  217. {
  218. column.Tapped?.Invoke(column, null);
  219. }
  220. else
  221. {
  222. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(Array.Empty<object?>()));
  223. }
  224. }
  225. }
  226. }