AvaloniaDataGrid.axaml.cs 8.3 KB

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