AvaloniaDataGrid.axaml.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 string SearchText { get; set; } = "";
  38. public bool CanSearch
  39. {
  40. get => GetValue(CanSearchProperty);
  41. set => SetValue(CanSearchProperty, value);
  42. }
  43. public IEnumerable ItemsSource
  44. {
  45. get => GetValue(ItemsSourceProperty);
  46. set => SetValue(ItemsSourceProperty, value);
  47. }
  48. public bool ShowRecordCount
  49. {
  50. get => GetValue(ShowRecordCountProperty);
  51. set => SetValue(ShowRecordCountProperty, value);
  52. }
  53. public bool RefreshVisible
  54. {
  55. get => GetValue(RefreshVisibleProperty);
  56. set => SetValue(RefreshVisibleProperty, value);
  57. }
  58. public SelectionMode SelectionMode
  59. {
  60. get => GetValue(SelectionModeProperty);
  61. set => SetValue(SelectionModeProperty, value);
  62. }
  63. public double RowHeight
  64. {
  65. get => GetValue(RowHeightProperty);
  66. set => SetValue(RowHeightProperty, value);
  67. }
  68. public int ItemCount { get; set; }
  69. public DateTime LastUpdated
  70. {
  71. get => GetValue(LastUpdatedProperty);
  72. set => SetValue(LastUpdatedProperty, value);
  73. }
  74. public AvaloniaDataGridColumns Columns { get; private set; }
  75. public IEnumerable<object?> SelectedItems => Grid.SelectedItems.Cast<object?>();
  76. public event EventHandler<AvaloniaDataGridSelectionChangedEventArgs>? SelectionChanged;
  77. public event EventHandler<AvaloniaDataGridRefreshRequestedEventArgs>? RefreshRequested;
  78. #region Static Constructor and Property Changed Handlers
  79. static AvaloniaDataGrid()
  80. {
  81. ItemsSourceProperty.Changed.AddClassHandler<AvaloniaDataGrid>(ItemsSource_Changed);
  82. LastUpdatedProperty.Changed.AddClassHandler<AvaloniaDataGrid>(LastUpdated_Changed);
  83. ShowRecordCountProperty.Changed.AddClassHandler<AvaloniaDataGrid>(ShowRecordCount_Changed);
  84. }
  85. private static void ShowRecordCount_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  86. {
  87. grid.UpdateSummaryRow();
  88. }
  89. private static void LastUpdated_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  90. {
  91. grid.UpdateSummaryRow();
  92. }
  93. private static void ItemsSource_Changed(AvaloniaDataGrid grid, AvaloniaPropertyChangedEventArgs args)
  94. {
  95. grid.Grid.ItemsSource = grid.ItemsSource;
  96. grid.Grid.CollectionView.CollectionChanged -= grid.CollectionView_CollectionChanged;
  97. grid.Grid.CollectionView.CollectionChanged += grid.CollectionView_CollectionChanged;
  98. grid.UpdateSummaryRow();
  99. }
  100. #endregion
  101. public AvaloniaDataGrid()
  102. {
  103. InitializeComponent();
  104. Columns = new AvaloniaDataGridColumns();
  105. Columns.Changed += Columns_Changed;
  106. }
  107. private void CollectionView_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  108. {
  109. ItemCount = (Grid.CollectionView as DataGridCollectionView)?.ItemCount ?? 0;
  110. }
  111. private void Columns_Changed(AvaloniaDataGridColumns columns)
  112. {
  113. Grid.Columns.Clear();
  114. foreach(var column in columns)
  115. {
  116. Grid.Columns.Add(column.CreateColumn());
  117. }
  118. // Summaries
  119. var searchableColumns = Columns.Any(x => x.Searchable);
  120. SearchBar.IsVisible = searchableColumns && CanSearch;
  121. }
  122. private void UpdateSummaryRow()
  123. {
  124. _lastUpdated.IsVisible = LastUpdated != DateTime.MinValue;
  125. _recordCount.Content = $" {ItemCount} records";
  126. _recordCount.IsVisible = ShowRecordCount && ItemsSource is IEnumerable;
  127. _recordCountBox.IsVisible = _recordCount.IsVisible || _lastUpdated.IsVisible;
  128. }
  129. public void ClearSelection()
  130. {
  131. Grid.SelectedItem = null;
  132. Grid.SelectedItems.Clear();
  133. }
  134. private bool DoSearch(object? item)
  135. {
  136. if (SearchText.IsNullOrWhiteSpace()) return true;
  137. if (item is null) return false;
  138. foreach(var column in Columns)
  139. {
  140. if(column.Filter(item, SearchText)) return true;
  141. }
  142. return false;
  143. }
  144. [RelayCommand]
  145. private void Search()
  146. {
  147. Grid.CollectionView.Filter = DoSearch;
  148. Grid.CollectionView.Refresh();
  149. UpdateSummaryRow();
  150. }
  151. [RelayCommand]
  152. private void Refresh()
  153. {
  154. RefreshRequested?.Invoke(this, new());
  155. Grid.CollectionView.Refresh();
  156. }
  157. private void DataGrid_Tapped(object sender, TappedEventArgs e)
  158. {
  159. if (SelectionMode == SelectionMode.Multiple) return;
  160. var position = e.GetPosition(Grid);
  161. var parent = (e.Source as Visual)?.GetVisualAncestors().Where(x => x is DataGridCell || x is DataGridColumnHeader).FirstOrDefault();
  162. if (parent is null) return;
  163. if(parent is DataGridCell cell)
  164. {
  165. var cellCollection = cell.GetVisualParent<DataGridCellsPresenter>();
  166. if (cellCollection is null) return;
  167. var colIdx = cellCollection.Children.IndexOf(cell);
  168. var row = cellCollection.GetVisualAncestors().OfType<DataGridRow>().FirstOrDefault();
  169. if (row is null) return;
  170. var rowCollection = row.GetVisualParent<DataGridRowsPresenter>();
  171. if (rowCollection is null) return;
  172. var rowIdx = rowCollection.Children.IndexOf(row);
  173. var item = (Grid.CollectionView as DataGridCollectionView)?[rowIdx];
  174. var column = Columns[colIdx];
  175. if(column.Tapped is not null)
  176. {
  177. column.Tapped?.Invoke(column, item);
  178. }
  179. else
  180. {
  181. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(new object?[] { item }));
  182. }
  183. }
  184. else if(parent is DataGridColumnHeader header)
  185. {
  186. var headerCollection = header.GetVisualParent<DataGridColumnHeadersPresenter>();
  187. if (headerCollection is null) return;
  188. var colIdx = headerCollection.Children.IndexOf(header);
  189. var column = Columns[colIdx];
  190. if(column.Tapped is not null)
  191. {
  192. column.Tapped?.Invoke(column, null);
  193. }
  194. else
  195. {
  196. SelectionChanged?.Invoke(this, new AvaloniaDataGridSelectionChangedEventArgs(Array.Empty<object?>()));
  197. }
  198. }
  199. }
  200. }