DynamicGridGridUIComponent.cs 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using InABox.WPF;
  4. using org.omg.PortableInterceptor;
  5. using Syncfusion.Data;
  6. using Syncfusion.Data.Extensions;
  7. using Syncfusion.UI.Xaml.Grid;
  8. using Syncfusion.UI.Xaml.Grid.Cells;
  9. using Syncfusion.UI.Xaml.Grid.Helpers;
  10. using Syncfusion.UI.Xaml.ScrollAxis;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.ComponentModel;
  15. using System.Data;
  16. using System.Diagnostics.CodeAnalysis;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using System.Windows.Threading;
  27. using DataRow = System.Data.DataRow;
  28. using TimeSpanEdit = Syncfusion.Windows.Shared.TimeSpanEdit;
  29. namespace InABox.DynamicGrid;
  30. public enum DynamicGridLines
  31. {
  32. Both,
  33. Horizontal,
  34. Vertical,
  35. None
  36. }
  37. public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynamicGridGridUIComponent<T>
  38. where T : BaseObject, new()
  39. {
  40. private readonly Dictionary<string, string> _filterpredicates = new();
  41. private Dictionary<string, CoreTable> Lookups = new();
  42. private IDynamicGridUIComponentParent<T> _parent;
  43. public IDynamicGridUIComponentParent<T> Parent
  44. {
  45. get => _parent;
  46. set
  47. {
  48. _parent = value;
  49. CellBackgroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellBackground);
  50. CellForegroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellForeground);
  51. CellFontSizeConverter = new DynamicGridCellStyleConverter<double?>(Parent, GetCellFontSize);
  52. CellFontStyleConverter = new DynamicGridCellStyleConverter<System.Windows.FontStyle?>(Parent, GetCellFontStyle);
  53. CellFontWeightConverter = new DynamicGridCellStyleConverter<System.Windows.FontWeight?>(Parent, GetCellFontWeight);
  54. DataGrid.RowStyleSelector = Parent.RowStyleSelector;
  55. DataGrid.TableSummaryRowStyleSelector = new SummaryRowStyleSelector(this, GetSummaryRowStyle);
  56. DataGrid.TableSummaryCellStyleSelector = new SummaryCellStyleSelector(this, GetSummaryCellStyle);
  57. }
  58. }
  59. FrameworkElement IDynamicGridUIComponent<T>.Control => DataGrid;
  60. #region IDynamicGridGridUIComponent
  61. IList<DynamicColumnBase> IDynamicGridGridUIComponent<T>.ColumnList => ColumnList;
  62. int IDynamicGridGridUIComponent<T>.RowHeight => (int)RowHeight;
  63. #endregion
  64. protected readonly SfDataGrid DataGrid;
  65. private readonly ContextMenu ColumnsMenu;
  66. private readonly GridRowSizingOptions gridRowResizingOptions = new() { CanIncludeHiddenColumns = false, AutoFitMode = AutoFitMode.Default };
  67. /// <summary>
  68. /// <see langword="null"/> when <see cref="DataGrid.ItemsSource"/> is <see langword="null"/>, generally while the grid is refreshing its columns.
  69. /// </summary>
  70. private DataTable? DataGridItems => (DataGrid.ItemsSource as DataTable);
  71. private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellBackgroundConverter;
  72. private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellForegroundConverter;
  73. private DynamicGridCellStyleConverter<double?> CellFontSizeConverter;
  74. private DynamicGridCellStyleConverter<System.Windows.FontStyle?> CellFontStyleConverter;
  75. private DynamicGridCellStyleConverter<System.Windows.FontWeight?> CellFontWeightConverter;
  76. #region Configuration
  77. private DynamicGridLines _gridLines = DynamicGridLines.Both;
  78. public DynamicGridLines GridLines
  79. {
  80. get => _gridLines;
  81. set
  82. {
  83. _gridLines = value;
  84. DataGrid.GridLinesVisibility = value switch
  85. {
  86. DynamicGridLines.Both => GridLinesVisibility.Both,
  87. DynamicGridLines.Vertical => GridLinesVisibility.Vertical,
  88. DynamicGridLines.Horizontal => GridLinesVisibility.Horizontal,
  89. _ => GridLinesVisibility.None,
  90. };
  91. }
  92. }
  93. public double RowHeight
  94. {
  95. get => DataGrid.RowHeight;
  96. set => DataGrid.RowHeight = value;
  97. }
  98. public double HeaderRowHeight
  99. {
  100. get => DataGrid.HeaderRowHeight;
  101. set => DataGrid.HeaderRowHeight = value;
  102. }
  103. #endregion
  104. public DynamicGridGridUIComponent()
  105. {
  106. ColumnsMenu = new ContextMenu();
  107. ColumnsMenu.Opened += ColumnsMenu_ContextMenuOpening;
  108. DataGrid = new SfDataGrid();
  109. DataGrid.VerticalAlignment = VerticalAlignment.Stretch;
  110. DataGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
  111. DataGrid.HeaderContextMenu = ColumnsMenu;
  112. DataGrid.CellTapped += DataGrid_CellTapped;
  113. DataGrid.CellDoubleTapped += DataGrid_CellDoubleTapped;
  114. DataGrid.SelectionChanging += DataGrid_SelectionChanging;
  115. DataGrid.SelectionChanged += DataGrid_SelectionChanged;
  116. DataGrid.SelectionMode = GridSelectionMode.Extended;
  117. DataGrid.SelectionUnit = GridSelectionUnit.Row;
  118. DataGrid.CanMaintainScrollPosition = true;
  119. DataGrid.SummaryCalculationUnit = SummaryCalculationUnit.AllRows;
  120. DataGrid.LiveDataUpdateMode = LiveDataUpdateMode.AllowSummaryUpdate;
  121. DataGrid.NavigationMode = NavigationMode.Row;
  122. DataGrid.AllowEditing = false;
  123. DataGrid.EditTrigger = EditTrigger.OnTap;
  124. DataGrid.CurrentCellBeginEdit += DataGrid_CurrentCellBeginEdit;
  125. DataGrid.CurrentCellEndEdit += DataGrid_CurrentCellEndEdit;
  126. DataGrid.CurrentCellDropDownSelectionChanged += DataGrid_CurrentCellDropDownSelectionChanged;
  127. DataGrid.PreviewKeyUp += DataGrid_PreviewKeyUp;
  128. DataGrid.BorderBrush = new SolidColorBrush(Colors.Gray);
  129. DataGrid.BorderThickness = new Thickness(0.75F);
  130. DataGrid.Background = new SolidColorBrush(Colors.DimGray);
  131. DataGrid.AutoGenerateColumns = false;
  132. DataGrid.ColumnSizer = GridLengthUnitType.AutoLastColumnFill;
  133. DataGrid.SelectionForegroundBrush = GetCellSelectionForegroundBrush() ?? DynamicGridUtils.SelectionForeground;
  134. DataGrid.RowSelectionBrush = GetCellSelectionBackgroundBrush() ?? DynamicGridUtils.SelectionBackground;
  135. DataGrid.AllowDraggingRows = false;
  136. DataGrid.Drop += DataGrid_Drop;
  137. DataGrid.DragOver += DataGrid_DragOver;
  138. DataGrid.RowDragDropTemplate = TemplateGenerator.CreateDataTemplate(() =>
  139. {
  140. var border = new Border();
  141. border.Width = 100;
  142. border.Height = 100;
  143. border.BorderBrush = new SolidColorBrush(Colors.Firebrick);
  144. border.Background = new SolidColorBrush(Colors.Red);
  145. border.CornerRadius = new CornerRadius(5);
  146. return border;
  147. });
  148. DataGrid.CurrentCellBorderThickness = new Thickness(0);
  149. DataGrid.AllowFiltering = false;
  150. DataGrid.EnableDataVirtualization = true;
  151. DataGrid.RowHeight = 30;
  152. DataGrid.QueryRowHeight += DataGrid_QueryRowHeight;
  153. DataGrid.HeaderRowHeight = 30;
  154. DataGrid.MouseLeftButtonUp += DataGrid_MouseLeftButtonUp;
  155. DataGrid.MouseRightButtonUp += DataGrid_MouseRightButtonUp;
  156. DataGrid.KeyUp += DataGrid_KeyUp;
  157. DataGrid.PreviewGotKeyboardFocus += DataGrid_PreviewGotKeyboardFocus;
  158. //DataGrid.SelectionController = new GridSelectionControllerExt(DataGrid);
  159. DataGrid.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Visible);
  160. DataGrid.FilterChanged += DataGrid_FilterChanged;
  161. DataGrid.FilterItemsPopulating += DataGrid_FilterItemsPopulating;
  162. var fltstyle = new Style(typeof(GridFilterControl));
  163. fltstyle.Setters.Add(new Setter(GridFilterControl.FilterModeProperty, FilterMode.Both));
  164. fltstyle.Setters.Add(new Setter(GridFilterControl.SortOptionVisibilityProperty, Visibility.Collapsed));
  165. DataGrid.FilterPopupStyle = fltstyle;
  166. //DataGrid.MouseMove += DataGrid_MouseMove;
  167. DataGrid.CellToolTipOpening += DataGrid_CellToolTipOpening;
  168. DataGrid.SizeChanged += DataGrid_SizeChanged;
  169. //DataGrid.CellRenderers.Remove("Numeric");
  170. //DataGrid.CellRenderers.Add("Numeric", new CustomNumericCellRenderer(this));
  171. //DataGrid.CellRenderers.Remove("TextBox");
  172. //DataGrid.CellRenderers.Add("TextBox", new CustomTextCellRenderer(this));
  173. }
  174. //private class CustomTextCellRenderer : GridCellTextBoxRenderer
  175. //{
  176. // private DynamicGridGridUIComponent<T> Component;
  177. // public CustomTextCellRenderer(DynamicGridGridUIComponent<T> component)
  178. // {
  179. // Component = component;
  180. // }
  181. // public override void OnInitializeEditElement(DataColumnBase dataColumn, TextBox uiElement, object dataContext)
  182. // {
  183. // base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
  184. // uiElement.TextChanged += UiElement_TextChanged;
  185. // }
  186. // private void UiElement_TextChanged(object sender, TextChangedEventArgs e)
  187. // {
  188. // throw new NotImplementedException();
  189. // }
  190. // private void UiElement_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  191. // {
  192. // Component.ChangeValue(e.OldValue, e.NewValue);
  193. // }
  194. //}
  195. //private class CustomNumericCellRenderer : GridCellNumericRenderer
  196. //{
  197. // private DynamicGridGridUIComponent<T> Component;
  198. // public CustomNumericCellRenderer(DynamicGridGridUIComponent<T> component)
  199. // {
  200. // Component = component;
  201. // }
  202. // public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)
  203. // {
  204. // base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
  205. // uiElement.ValueChanged += UiElement_ValueChanged;
  206. // }
  207. // private void UiElement_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  208. // {
  209. // Component.ChangeValue(e.OldValue, e.NewValue);
  210. // }
  211. //}
  212. private void ChangeValue(object oldValue, object newValue)
  213. {
  214. }
  215. private void ColumnsMenu_ContextMenuOpening(object sender, RoutedEventArgs e)
  216. {
  217. if (sender is not ContextMenu menu) return;
  218. menu.Items.Clear();
  219. Parent.LoadColumnsMenu(menu);
  220. }
  221. #region Selection
  222. public CoreRow[] SelectedRows
  223. {
  224. get => GetSelectedRows();
  225. set => SetSelectedRows(value);
  226. }
  227. private CoreRow[] GetSelectedRows()
  228. {
  229. var result = new List<CoreRow>();
  230. foreach (var item in DataGrid.SelectedItems)
  231. {
  232. if (item is DataRowView datarow)
  233. {
  234. var row = datarow.Row.Table.Rows.IndexOf(datarow.Row);
  235. result.Add(Parent.Data.Rows[row]);
  236. }
  237. }
  238. return result.ToArray();
  239. }
  240. private void SetSelectedRows(CoreRow[] rows)
  241. {
  242. DataGrid.SelectedItems.Clear();
  243. var table = DataGridItems;
  244. if(table is null) return;
  245. var dataRows = rows.Where(x => x.Index > -1).Select(row =>
  246. {
  247. return table.Rows[row.Index];
  248. });
  249. if (!Parent.Options.MultiSelect)
  250. {
  251. dataRows = dataRows.Take(1);
  252. }
  253. foreach (var row in dataRows)
  254. {
  255. var record = DataGrid.View?.Records.FirstOrDefault(x => (x.Data as DataRowView)!.Row == row);
  256. if(record?.Data is DataRowView rowView)
  257. {
  258. DataGrid.SelectedItems.Add(rowView);
  259. }
  260. }
  261. }
  262. private void DataGrid_SelectionChanging(object? sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangingEventArgs e)
  263. {
  264. var cancel = new CancelEventArgs();
  265. Parent.BeforeSelection(cancel);
  266. if (cancel.Cancel)
  267. {
  268. e.Cancel = true;
  269. }
  270. }
  271. private void DataGrid_SelectionChanged(object? sender, GridSelectionChangedEventArgs e)
  272. {
  273. if(Parent.IsReady && !Parent.IsRefreshing)
  274. {
  275. StartTimer();
  276. }
  277. }
  278. private DispatcherTimer? clicktimer;
  279. private void StartTimer()
  280. {
  281. if (clicktimer is null)
  282. {
  283. clicktimer = new DispatcherTimer();
  284. clicktimer.Interval = TimeSpan.FromMilliseconds(200);
  285. clicktimer.Tick += (o, e) =>
  286. {
  287. clicktimer.IsEnabled = false;
  288. Parent.SelectItems(SelectedRows);
  289. };
  290. }
  291. clicktimer.IsEnabled = true;
  292. }
  293. private void StopTimer()
  294. {
  295. if (clicktimer is not null)
  296. clicktimer.IsEnabled = false;
  297. }
  298. #endregion
  299. public CoreRow[] GetVisibleRows()
  300. {
  301. var items = DataGrid.ItemsSource;
  302. var result = new List<CoreRow>();
  303. var table = DataGridItems;
  304. if (table is null) return Array.Empty<CoreRow>();
  305. var rows = DataGrid.View.Records.Select(x => (x.Data as DataRowView)!).ToList();
  306. foreach (var row in rows)
  307. {
  308. var iRow = table.Rows.IndexOf(row.Row);
  309. result.Add(Parent.Data.Rows[iRow]);
  310. }
  311. return result.ToArray();
  312. }
  313. private bool bFilterVisible;
  314. private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
  315. {
  316. if (!DataGrid.IsEnabled)
  317. return;
  318. var visualContainer = DataGrid.GetVisualContainer();
  319. var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
  320. var columnindex = DataGrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex);
  321. var column = GetColumn(columnindex);
  322. if(column is not null)
  323. {
  324. Parent.OpenColumnMenu(column);
  325. }
  326. }
  327. private void DataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  328. {
  329. if (!DataGrid.IsEnabled)
  330. return;
  331. var visualContainer = DataGrid.GetVisualContainer();
  332. var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
  333. var columnindex = DataGrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex);
  334. // Header Click Here!
  335. if (rowcolumnindex.RowIndex < DataGrid.StackedHeaderRows.Count + 1)
  336. {
  337. var column = GetColumn(columnindex);
  338. if(column is DynamicActionColumn dac)
  339. {
  340. Parent.ExecuteActionColumn(dac, null);
  341. }
  342. }
  343. else if (!bFilterVisible)
  344. {
  345. StartTimer();
  346. }
  347. }
  348. private void DataGrid_CellTapped(object? sender, GridCellTappedEventArgs e)
  349. {
  350. if (!DataGrid.IsEnabled)
  351. return;
  352. if (GetColumn(e.RowColumnIndex.ColumnIndex) is DynamicActionColumn dac)
  353. {
  354. if(e.ChangedButton == MouseButton.Left || (e.ChangedButton == MouseButton.Right && dac is DynamicMenuColumn))
  355. {
  356. Parent.ExecuteActionColumn(dac, SelectedRows);
  357. }
  358. }
  359. else
  360. {
  361. StartTimer();
  362. }
  363. }
  364. private void DataGrid_KeyUp(object sender, KeyEventArgs e)
  365. {
  366. if (sender != DataGrid) return;
  367. Parent.HandleKey(e);
  368. }
  369. private void DataGrid_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
  370. {
  371. var bOld = bFilterVisible;
  372. if (e.NewFocus is GridFilterControl)
  373. bFilterVisible = true;
  374. else if (e.NewFocus is ScrollViewer || e.NewFocus is SfDataGrid)
  375. bFilterVisible = false;
  376. if (bOld && !bFilterVisible)
  377. {
  378. Parent.SelectItems(SelectedRows);
  379. }
  380. }
  381. // Please always use this function where possible!
  382. /// <summary>
  383. /// Get the <see cref="CoreRow"/> associated with a <paramref name="rowIndex"/> given from the Syncfusion DataGrid.
  384. /// </summary>
  385. /// <remarks>
  386. /// This is mandatory to use whenever we want the data associated with an index which Syncfusion has given us,
  387. /// because filtering and sorting will cause normal indexing operations to fail.
  388. /// </remarks>
  389. /// <param name="rowIndex"></param>
  390. /// <returns></returns>
  391. private CoreRow? GetRowFromIndex(int rowIndex)
  392. {
  393. // Syncfusion has given us the row index, so it also will give us the correct row, after sorting.
  394. // Hence, here we use the syncfusion DataGrid.GetRecordAtRowIndex, which *should* always return a DataRowView.
  395. var row = DataGrid.GetRecordAtRowIndex(rowIndex);
  396. if (row is not DataRowView dataRowView || DataGridItems is not DataTable table)
  397. return null;
  398. var rowIdx = table.Rows.IndexOf(dataRowView.Row);
  399. if (rowIdx < 0)
  400. return null;
  401. return Parent.Data.Rows[rowIdx];
  402. }
  403. private void DataGrid_CellDoubleTapped(object? sender, GridCellDoubleTappedEventArgs e)
  404. {
  405. StopTimer();
  406. Parent.DoubleClickCell(GetRowFromIndex(e.RowColumnIndex.RowIndex), GetColumn(e.RowColumnIndex.ColumnIndex));
  407. }
  408. private void DataGrid_QueryRowHeight(object? sender, QueryRowHeightEventArgs e)
  409. {
  410. if (e.RowIndex > 0)
  411. {
  412. e.Height = DataGrid.RowHeight;
  413. if (DataGrid.GridColumnSizer.GetAutoRowHeight(e.RowIndex, gridRowResizingOptions, out var autoHeight))
  414. if (autoHeight > DataGrid.RowHeight)
  415. e.Height = autoHeight;
  416. e.Handled = true;
  417. }
  418. }
  419. private void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
  420. {
  421. if (Parent.IsReady && !Parent.IsRefreshing)
  422. ResizeColumns(DataGrid, e.NewSize.Width - 2, e.NewSize.Height - 2);
  423. }
  424. public bool OptionsChanged()
  425. {
  426. ColumnsMenu.Visibility = Parent.Options.SelectColumns ? Visibility.Visible : Visibility.Hidden;
  427. var allowEditing = Parent.IsDirectEditMode();
  428. var reloadColumns = false;
  429. if (DataGrid.AllowEditing != allowEditing)
  430. {
  431. DataGrid.NavigationMode = allowEditing ? NavigationMode.Cell : NavigationMode.Row;
  432. DataGrid.AllowEditing = allowEditing;
  433. reloadColumns = true;
  434. }
  435. DataGrid.AllowFiltering = Parent.Options.FilterRows;
  436. DataGrid.FilterRowPosition = Parent.Options.FilterRows ? FilterRowPosition.FixedTop : FilterRowPosition.None;
  437. if (Parent.Options.DragSource)
  438. {
  439. if (!DataGrid.AllowDraggingRows)
  440. {
  441. DataGrid.AllowDraggingRows = true;
  442. DataGrid.RowDragDropController.DragStart += RowDragDropController_DragStart;
  443. }
  444. }
  445. else
  446. {
  447. if (DataGrid.AllowDraggingRows)
  448. {
  449. DataGrid.AllowDraggingRows = false;
  450. DataGrid.RowDragDropController.DragStart -= RowDragDropController_DragStart;
  451. }
  452. }
  453. DataGrid.AllowDrop = Parent.Options.DragTarget;
  454. DataGrid.SelectionMode = Parent.Options.MultiSelect ? GridSelectionMode.Extended : GridSelectionMode.Single;
  455. return reloadColumns && DataGrid.Columns.Count > 0;
  456. }
  457. private void DataGrid_FilterChanged(object? o, GridFilterEventArgs e)
  458. {
  459. var col = DataGrid.Columns.IndexOf(e.Column);
  460. if (GetColumn(col) is DynamicActionColumn column)
  461. {
  462. if (e.FilterPredicates != null)
  463. {
  464. var filter = e.FilterPredicates.Select(x => x.FilterValue.ToString()!).ToArray();
  465. var include = e.FilterPredicates.Any(x => x.FilterType == FilterType.Equals);
  466. if (include)
  467. {
  468. column.SelectedFilters = filter;
  469. column.ExcludeFilters = null;
  470. }
  471. else if(column.Filters is not null)
  472. {
  473. column.SelectedFilters = column.Filters.Except(filter).ToArray();
  474. column.ExcludeFilters = null;
  475. }
  476. else
  477. {
  478. column.SelectedFilters = null;
  479. column.ExcludeFilters = filter;
  480. }
  481. }
  482. else
  483. {
  484. column.SelectedFilters = null;
  485. column.ExcludeFilters = null;
  486. }
  487. //DataGrid.ClearFilter(e.Column);
  488. //e.FilterPredicates?.Clear();
  489. //e.FilterPredicates?.Add(new FilterPredicate() { PredicateType = PredicateType.Or, FilterBehavior = Syncfusion.Data.FilterBehavior.StringTyped, FilterMode = ColumnFilter.DisplayText, FilterType = Syncfusion.Data.FilterType.NotEquals, FilterValue = "" });
  490. //e.FilterPredicates?.Add(new FilterPredicate() { PredicateType = PredicateType.Or, FilterBehavior = Syncfusion.Data.FilterBehavior.StringTyped, FilterMode = ColumnFilter.DisplayText, FilterType = Syncfusion.Data.FilterType.Equals, FilterValue = "" });
  491. //Parent.Refresh(false, false);
  492. e.Handled = true;
  493. }
  494. if (e.FilterPredicates == null)
  495. {
  496. if (_filterpredicates.ContainsKey(e.Column.MappingName))
  497. _filterpredicates.Remove(e.Column.MappingName);
  498. }
  499. else
  500. {
  501. _filterpredicates[e.Column.MappingName] = Serialization.Serialize(e.FilterPredicates, true);
  502. }
  503. Parent.UIFilterChanged(this);
  504. UpdateRecordCount();
  505. }
  506. private void UpdateRecordCount()
  507. {
  508. var count = DataGrid.View != null ? DataGrid.View.Records.Count : Parent.Data.Rows.Count;
  509. Parent.UpdateRecordCount(count);
  510. }
  511. private void DataGrid_FilterItemsPopulating(object? sender, GridFilterItemsPopulatingEventArgs e)
  512. {
  513. var colIdx = DataGrid.Columns.IndexOf(e.Column);
  514. var column = GetColumn(colIdx);
  515. if(column is not null)
  516. {
  517. var filterItems = Parent.GetColumnFilterItems(column);
  518. if(filterItems is not null)
  519. {
  520. e.ItemsSource = filterItems.Select(x =>
  521. {
  522. var element = new FilterElement
  523. {
  524. DisplayText = x,
  525. ActualValue = x,
  526. };
  527. if(column is DynamicActionColumn dac)
  528. {
  529. element.IsSelected = (dac.SelectedFilters is null || dac.SelectedFilters.Contains(x))
  530. && (dac.ExcludeFilters is null || !dac.ExcludeFilters.Contains(x));
  531. }
  532. return element;
  533. });
  534. }
  535. else if (column is DynamicActionColumn dac && dac.Filters is not null)
  536. {
  537. e.ItemsSource = dac.Filters.Select(x => new FilterElement
  538. {
  539. DisplayText = x,
  540. ActualValue = x,
  541. IsSelected = (dac.SelectedFilters is null || dac.SelectedFilters.Contains(x))
  542. && (dac.ExcludeFilters is null || !dac.ExcludeFilters.Contains(x))
  543. });
  544. }
  545. }
  546. }
  547. private void DataGrid_CellToolTipOpening(object? sender, GridCellToolTipOpeningEventArgs e)
  548. {
  549. if (GetColumn(e.RowColumnIndex.ColumnIndex) is not DynamicActionColumn col)
  550. return;
  551. var toolTip = col.ToolTip;
  552. if (toolTip is null)
  553. return;
  554. var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
  555. e.ToolTip.Template = TemplateGenerator.CreateControlTemplate(
  556. typeof(ToolTip),
  557. () => toolTip.Invoke(col, row)
  558. );
  559. }
  560. public void AddVisualFilter(string column, string value, FilterType filtertype = FilterType.Contains)
  561. {
  562. if (string.IsNullOrWhiteSpace(value))
  563. return;
  564. var col = DataGrid.Columns.FirstOrDefault((x=>String.Equals(x.MappingName?.ToUpper(),column?.Replace(".", "_").ToUpper())));
  565. if (col != null)
  566. {
  567. col.FilterPredicates.Add(new FilterPredicate { FilterType = filtertype, FilterValue = value });
  568. col.FilteredFrom = FilteredFrom.FilterRow;
  569. }
  570. }
  571. public List<Tuple<string, Func<CoreRow, bool>>> GetFilterPredicates()
  572. {
  573. var list = new List<Tuple<string, Func<CoreRow, bool>>>();
  574. foreach (var column in DataGrid.Columns)
  575. {
  576. var colIndex = DataGrid.Columns.IndexOf(column);
  577. var col = ColumnList[colIndex];
  578. if (col is DynamicGridColumn gridColumn)
  579. {
  580. var rowPredicate = DynamicGridGridUIComponentExtension.ConvertColumnPredicates(gridColumn, column.FilterPredicates);
  581. if(rowPredicate is not null)
  582. {
  583. list.Add(new(gridColumn.ColumnName, rowPredicate));
  584. }
  585. }
  586. else if(col is DynamicActionColumn dac && dac.FilterRecord is not null)
  587. {
  588. if(dac.SelectedFilters is not null && dac.SelectedFilters.Length > 0)
  589. {
  590. list.Add(new(column.MappingName, (row) => dac.FilterRecord(row, dac.SelectedFilters)));
  591. }
  592. if(dac.ExcludeFilters is not null && dac.ExcludeFilters.Length > 0)
  593. {
  594. list.Add(new(column.MappingName, (row) => !dac.FilterRecord(row, dac.ExcludeFilters)));
  595. }
  596. }
  597. }
  598. return list;
  599. }
  600. public void ScrollIntoView(CoreRow row)
  601. {
  602. var rowIdx = DataGrid.ResolveToRowIndex(row.Index + 1);
  603. DataGrid.ScrollInView(new RowColumnIndex(rowIdx, 0));
  604. }
  605. protected virtual Brush? GetCellSelectionForegroundBrush() => DynamicGridUtils.SelectionForeground;
  606. protected virtual Brush? GetCellSelectionBackgroundBrush() => DynamicGridUtils.SelectionBackground;
  607. protected virtual Brush? GetCellBackground(CoreRow row, DynamicColumnBase column) => null;
  608. protected virtual Brush? GetCellForeground(CoreRow row, DynamicColumnBase column) => null;
  609. protected virtual double? GetCellFontSize(CoreRow row, DynamicColumnBase column) => null;
  610. protected virtual FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column) => null;
  611. protected virtual FontWeight? GetCellFontWeight(CoreRow row, DynamicColumnBase column) => null;
  612. protected virtual Style GetHeaderCellStyle(DynamicColumnBase column)
  613. {
  614. var headStyle = new Style(typeof(GridHeaderCellControl));
  615. headStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  616. headStyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
  617. headStyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
  618. if(column is DynamicActionColumn actionColumn)
  619. {
  620. headStyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0)));
  621. headStyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
  622. if(column is DynamicImageColumn imgCol)
  623. {
  624. if (imgCol.HeaderText.IsNullOrWhiteSpace())
  625. {
  626. var image = imgCol.Image?.Invoke(null);
  627. if (image != null)
  628. {
  629. var template = new ControlTemplate(typeof(GridHeaderCellControl));
  630. var border = new FrameworkElementFactory(typeof(Border));
  631. border.SetValue(Border.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro));
  632. border.SetValue(Border.PaddingProperty, new Thickness(4));
  633. var img = new FrameworkElementFactory(typeof(Image));
  634. img.SetValue(Image.SourceProperty, image);
  635. border.AppendChild(img);
  636. template.VisualTree = border;
  637. headStyle.Setters.Add(new Setter(Control.TemplateProperty, template));
  638. }
  639. }
  640. }
  641. if (actionColumn.VerticalHeader && !actionColumn.HeaderText.IsNullOrWhiteSpace())
  642. {
  643. headStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
  644. headStyle.Setters.Add(new Setter(Control.TemplateProperty,
  645. Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
  646. }
  647. }
  648. return headStyle;
  649. }
  650. protected virtual Style GetSummaryRowStyle()
  651. {
  652. var style = new Style(typeof(TableSummaryRowControl));
  653. return style;
  654. }
  655. protected virtual Style GetSummaryCellStyle(DynamicColumnBase column)
  656. {
  657. var style = new Style(typeof(GridTableSummaryCell));
  658. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  659. style.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
  660. if(column is DynamicGridColumn gridColumn)
  661. {
  662. style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty,
  663. column != null ? gridColumn.HorizontalAlignment(typeof(double)) : HorizontalAlignment.Right));
  664. }
  665. else
  666. {
  667. style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Right));
  668. }
  669. style.Setters.Add(new Setter(Control.BorderBrushProperty, new SolidColorBrush(Colors.Gray)));
  670. style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0, 0, 0.75, 0)));
  671. style.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
  672. style.Setters.Add(new Setter(Control.FontWeightProperty, FontWeights.DemiBold));
  673. return style;
  674. }
  675. #region Columns
  676. private class StackedHeaderRenderer : GridStackedHeaderCellRenderer
  677. {
  678. private Style Style;
  679. public StackedHeaderRenderer()
  680. {
  681. var headstyle = new Style(typeof(GridStackedHeaderCellControl));
  682. headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  683. headstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
  684. headstyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
  685. headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0, 0.0, 0, 0)));
  686. headstyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
  687. Style = headstyle;
  688. }
  689. public override void OnInitializeEditElement(DataColumnBase dataColumn, GridStackedHeaderCellControl uiElement, object dataContext)
  690. {
  691. uiElement.Style = Style;
  692. base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
  693. }
  694. }
  695. private void LoadStackedHeaders(DynamicGridColumnGroupings groupings)
  696. {
  697. DataGrid.StackedHeaderRows.Clear();
  698. foreach(var grouping in groupings)
  699. {
  700. var row = new StackedHeaderRow();
  701. var i = 0;
  702. foreach(var group in grouping.Groups)
  703. {
  704. var start = Math.Max(i, ColumnList.IndexOf(group.StartColumn));
  705. var end = Math.Max(start, ColumnList.IndexOf(group.EndColumn));
  706. if(end < start)
  707. {
  708. i = end + 1;
  709. continue;
  710. }
  711. var cols = Enumerable.Range(start, end - start + 1).Select(i => DataGrid.Columns[i]).ToArray();
  712. var stackedColumn = new StackedColumn
  713. {
  714. HeaderText = group.Header,
  715. ChildColumns = string.Join(',', cols.Select(x => x.MappingName))
  716. };
  717. row.StackedColumns.Add(stackedColumn);
  718. i = end + 1;
  719. }
  720. DataGrid.StackedHeaderRows.Add(row);
  721. }
  722. if(groupings.Count > 0)
  723. {
  724. DataGrid.CellRenderers.Remove("StackedHeader");
  725. DataGrid.CellRenderers.Add("StackedHeader", new StackedHeaderRenderer());
  726. }
  727. }
  728. private readonly List<DynamicColumnBase> ColumnList = new();
  729. private List<DynamicActionColumn> ActionColumns = new();
  730. private DynamicColumnBase? GetColumn(int index) =>
  731. index >= 0 && index < ColumnList.Count ? ColumnList[index] : null;
  732. int IDynamicGridUIComponent<T>.DesiredWidth()
  733. {
  734. return this.DesiredWidth();
  735. }
  736. private void ResizeColumns(SfDataGrid grid, double width, double height)
  737. {
  738. if (Parent.Data == null || width <= 0)
  739. return;
  740. grid.Dispatcher.BeginInvoke(() =>
  741. {
  742. foreach (var (index, size) in this.CalculateColumnSizes(width))
  743. DataGrid.Columns[index].Width = Math.Max(0.0F, size);
  744. });
  745. }
  746. private ObservableCollection<ISummaryColumn> Summaries = new();
  747. private void LoadActionColumns(DynamicActionColumnPosition position)
  748. {
  749. for (var i = 0; i < ActionColumns.Count; i++)
  750. {
  751. var column = ActionColumns[i];
  752. if (column.Position == position)
  753. {
  754. //String sColName = String.Format("ActionColumn{0}{1}", i, position == DynamicActionColumnPosition.Start ? "L" : "R");
  755. var sColName = string.Format("ActionColumn{0}", i);
  756. gridRowResizingOptions.ExcludeColumns.Add(sColName);
  757. var summary = column.Summary();
  758. if (summary != null)
  759. {
  760. summary.Name = sColName;
  761. summary.MappingName = sColName;
  762. Summaries.Add(summary);
  763. }
  764. if (column is DynamicImageColumn imgcol)
  765. {
  766. var newcol = new GridImageColumn();
  767. newcol.MappingName = sColName;
  768. //newcol.Stretch = Stretch.Uniform;
  769. newcol.Width = column.Width == 0 ? DataGrid.RowHeight : column.Width;
  770. newcol.Padding = new Thickness(4);
  771. newcol.ImageHeight = DataGrid.RowHeight - 8;
  772. newcol.ImageWidth = DataGrid.RowHeight - 8;
  773. newcol.ColumnSizer = GridLengthUnitType.None;
  774. newcol.HeaderText = column.HeaderText;
  775. ApplyFilterStyle(newcol, true, true);
  776. newcol.ShowToolTip = column.ToolTip != null;
  777. newcol.ShowHeaderToolTip = column.ToolTip != null;
  778. var style = new Style();
  779. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  780. style.Setters.Add(new Setter(Control.IsEnabledProperty, false));
  781. newcol.FilterRowCellStyle = style;
  782. newcol.HeaderStyle = GetHeaderCellStyle(column);
  783. DataGrid.Columns.Add(newcol);
  784. ColumnList.Add(column);
  785. }
  786. else if (column is DynamicTextColumn txtCol)
  787. {
  788. var newcol = new GridTextColumn();
  789. gridRowResizingOptions.ExcludeColumns.Add(sColName);
  790. newcol.TextWrapping = TextWrapping.NoWrap;
  791. newcol.TextAlignment = txtCol.Alignment == Alignment.NotSet
  792. ? TextAlignment.Left
  793. : txtCol.Alignment == Alignment.BottomLeft || txtCol.Alignment == Alignment.MiddleLeft ||
  794. txtCol.Alignment == Alignment.TopLeft
  795. ? TextAlignment.Left
  796. : txtCol.Alignment == Alignment.BottomCenter || txtCol.Alignment == Alignment.MiddleCenter ||
  797. txtCol.Alignment == Alignment.TopCenter
  798. ? TextAlignment.Center
  799. : TextAlignment.Right;
  800. newcol.AllowEditing = false;
  801. newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
  802. newcol.MappingName = sColName;
  803. newcol.Width = column.Width;
  804. newcol.ColumnSizer = GridLengthUnitType.None;
  805. newcol.HeaderText = column.HeaderText;
  806. newcol.AllowFiltering = (column.Filters != null && column.Filters.Length != 0) || column.FilterRecord is not null;
  807. newcol.AllowSorting = false;
  808. newcol.FilterRowOptionsVisibility = Visibility.Collapsed;
  809. newcol.ShowHeaderToolTip = column.ToolTip != null;
  810. newcol.ShowToolTip = column.ToolTip != null;
  811. newcol.ShowHeaderToolTip = column.ToolTip != null;
  812. var style = new Style();
  813. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  814. style.Setters.Add(new Setter(Control.IsEnabledProperty, false));
  815. newcol.FilterRowCellStyle = style;
  816. newcol.HeaderStyle = GetHeaderCellStyle(column);
  817. var cellstyle = new Style();
  818. cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
  819. new Binding()
  820. {
  821. Path = new PropertyPath("."), Converter = CellBackgroundConverter,
  822. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  823. }));
  824. cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
  825. new Binding()
  826. {
  827. Converter = CellForegroundConverter,
  828. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  829. }));
  830. cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
  831. new Binding()
  832. {
  833. Converter = CellFontSizeConverter,
  834. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  835. }));
  836. cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
  837. new Binding()
  838. {
  839. Converter = CellFontStyleConverter,
  840. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  841. }));
  842. cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
  843. new Binding()
  844. {
  845. Converter = CellFontWeightConverter,
  846. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  847. }));
  848. newcol.CellStyle = cellstyle;
  849. DataGrid.Columns.Add(newcol);
  850. ColumnList.Add(column);
  851. }
  852. else if (column is DynamicTemplateColumn tmplCol)
  853. {
  854. var newcol = new GridTemplateColumn();
  855. newcol.CellTemplateSelector = new TemplateColumnSelector(this, tmplCol.Template);
  856. newcol.AllowEditing = false;
  857. newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
  858. newcol.MappingName = sColName;
  859. newcol.Width = tmplCol.Width;
  860. newcol.ColumnSizer = GridLengthUnitType.None;
  861. newcol.HeaderText = column.HeaderText;
  862. newcol.AllowFiltering = false;
  863. newcol.AllowSorting = false;
  864. newcol.FilterRowOptionsVisibility = Visibility.Collapsed;
  865. newcol.ShowToolTip = column.ToolTip != null;
  866. newcol.ShowHeaderToolTip = column.ToolTip != null;
  867. var style = new Style();
  868. style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  869. style.Setters.Add(new Setter(Control.IsEnabledProperty, false));
  870. newcol.FilterRowCellStyle = style;
  871. newcol.HeaderStyle = GetHeaderCellStyle(column);
  872. var cellstyle = new Style();
  873. cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
  874. new Binding()
  875. {
  876. Path = new PropertyPath("."),
  877. Converter = CellBackgroundConverter,
  878. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  879. }));
  880. cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
  881. new Binding()
  882. {
  883. Converter = CellForegroundConverter,
  884. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  885. }));
  886. cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
  887. new Binding()
  888. {
  889. Converter = CellFontSizeConverter,
  890. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  891. }));
  892. cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
  893. new Binding()
  894. {
  895. Converter = CellFontStyleConverter,
  896. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  897. }));
  898. cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
  899. new Binding()
  900. {
  901. Converter = CellFontWeightConverter,
  902. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  903. }));
  904. newcol.CellStyle = cellstyle;
  905. DataGrid.Columns.Add(newcol);
  906. ColumnList.Add(column);
  907. }
  908. }
  909. }
  910. }
  911. public class SummaryRowStyleSelector(DynamicGridGridUIComponent<T> parent, Func<Style> selector) : StyleSelector
  912. {
  913. public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
  914. public Func<Style> Selector { get; set; } = selector;
  915. public override Style? SelectStyle(object item, DependencyObject container)
  916. {
  917. return Selector();
  918. }
  919. }
  920. public class SummaryCellStyleSelector(DynamicGridGridUIComponent<T> parent, Func<DynamicColumnBase, Style> selector) : StyleSelector
  921. {
  922. public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
  923. public Func<DynamicColumnBase, Style> Selector { get; set; } = selector;
  924. public override Style? SelectStyle(object item, DependencyObject container)
  925. {
  926. var vcol = ((GridTableSummaryCell)container).ColumnBase.ColumnIndex;
  927. var col = Parent.GetColumn(vcol);
  928. return col is not null ? Selector(col) : null;
  929. }
  930. }
  931. public class TemplateColumnSelector(DynamicGridGridUIComponent<T> parent, Func<CoreRow, FrameworkElement?> dataTemplate) : DataTemplateSelector
  932. {
  933. public Func<CoreRow, FrameworkElement?> DataTemplate { get; init; } = dataTemplate;
  934. public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
  935. public override DataTemplate? SelectTemplate(object item, DependencyObject container)
  936. {
  937. if (item is not DataRowView) return null;
  938. CoreRow? row;
  939. if(item is DataRowView view && Parent.DataGridItems is DataTable table)
  940. {
  941. var rowIdx = table.Rows.IndexOf(view.Row);
  942. if (rowIdx < 0)
  943. {
  944. row = null;
  945. }
  946. else
  947. {
  948. row = Parent.Parent.Data.Rows[rowIdx];
  949. }
  950. }
  951. else
  952. {
  953. row = null;
  954. }
  955. if (row is null) return null;
  956. return TemplateGenerator.CreateDataTemplate(() =>
  957. {
  958. return DataTemplate(row);
  959. });
  960. }
  961. }
  962. private void ApplyFilterStyle(GridColumn column, bool filtering, bool isactioncolumn)
  963. {
  964. var filterstyle = new Style();
  965. if (filtering)
  966. {
  967. filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, DynamicGridUtils.FilterBackground));
  968. column.ImmediateUpdateColumnFilter = true;
  969. column.ColumnFilter = ColumnFilter.Value;
  970. column.FilterRowCondition = FilterRowCondition.Contains;
  971. column.FilterRowOptionsVisibility = Visibility.Collapsed;
  972. column.AllowBlankFilters = true;
  973. column.AllowSorting = !isactioncolumn && Parent.CanSort();
  974. }
  975. else
  976. {
  977. filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
  978. filterstyle.Setters.Add(new Setter(Control.IsEnabledProperty, false));
  979. column.ColumnFilter = ColumnFilter.Value;
  980. column.AllowFiltering = false;
  981. column.AllowSorting = false;
  982. column.FilterRowEditorType = "TextBox";
  983. column.FilterRowOptionsVisibility = Visibility.Collapsed;
  984. }
  985. column.FilterRowCellStyle = filterstyle;
  986. }
  987. private void LoadDataColumns(DynamicGridColumns columns)
  988. {
  989. foreach (var column in columns)
  990. {
  991. if (this.CreateEditorColumn(column, out var newcol, out var prop))
  992. {
  993. newcol.GetEntity = () => _editingObject.Object;
  994. newcol.EntityChanged += DoEntityChanged;
  995. if (!newcol.VariableHeight)
  996. gridRowResizingOptions.ExcludeColumns.Add(newcol.MappingName);
  997. var newColumn = newcol.CreateGridColumn();
  998. newColumn.AllowEditing = newcol.Editable && Parent.IsDirectEditMode();
  999. var summary = newcol.Summary();
  1000. if (summary != null)
  1001. Summaries.Add(summary);
  1002. ApplyFilterStyle(newColumn, newcol.Filtered, false);
  1003. newColumn.HeaderStyle = GetHeaderCellStyle(column);
  1004. var cellstyle = new Style();
  1005. if (Parent.IsDirectEditMode())
  1006. {
  1007. if (prop.Editor is null || !prop.Editor.Editable.IsDirectEditable())
  1008. {
  1009. cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
  1010. new Binding()
  1011. {
  1012. Path = new PropertyPath("."), Converter = CellBackgroundConverter,
  1013. ConverterParameter = new DynamicGridCellStyleParameters(column,new SolidColorBrush(Colors.WhiteSmoke))
  1014. }));
  1015. newColumn.AllowEditing = false;
  1016. }
  1017. else
  1018. {
  1019. cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
  1020. new Binding()
  1021. {
  1022. Path = new PropertyPath("."), Converter = CellBackgroundConverter,
  1023. ConverterParameter = new DynamicGridCellStyleParameters(column,new SolidColorBrush(Colors.LightYellow))
  1024. }));
  1025. newColumn.AllowEditing = true;
  1026. }
  1027. cellstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
  1028. newColumn.CellStyle = cellstyle;
  1029. }
  1030. else
  1031. {
  1032. cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
  1033. new Binding()
  1034. {
  1035. Path = new PropertyPath("."), Converter = CellBackgroundConverter,
  1036. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  1037. }));
  1038. cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
  1039. new Binding()
  1040. {
  1041. Converter = CellForegroundConverter,
  1042. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  1043. }));
  1044. cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
  1045. new Binding()
  1046. {
  1047. Converter = CellFontSizeConverter,
  1048. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  1049. }));
  1050. cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
  1051. new Binding()
  1052. {
  1053. Converter = CellFontStyleConverter,
  1054. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  1055. }));
  1056. cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
  1057. new Binding()
  1058. {
  1059. Converter = CellFontWeightConverter,
  1060. ConverterParameter = new DynamicGridCellStyleParameters(column,DependencyProperty.UnsetValue)
  1061. }));
  1062. newColumn.CellStyle = cellstyle;
  1063. }
  1064. DataGrid.Columns.Add(newColumn);
  1065. ColumnList.Add(column);
  1066. foreach (var extra in newcol.ExtraColumns)
  1067. Parent.AddHiddenColumn(extra);
  1068. }
  1069. }
  1070. }
  1071. private void LoadSummaries()
  1072. {
  1073. if (Summaries.Any())
  1074. {
  1075. DataGrid.CellRenderers.Remove("TableSummary");
  1076. DataGrid.CellRenderers.Add("TableSummary", new DynamicGridAggregateRenderer());
  1077. DataGrid.TableSummaryRows.Add(new GridTableSummaryRow
  1078. {
  1079. ShowSummaryInRow = false,
  1080. Position = TableSummaryRowPosition.Bottom,
  1081. SummaryColumns = Summaries,
  1082. });
  1083. }
  1084. }
  1085. public void RefreshColumns(DynamicGridColumns columns, DynamicActionColumns actionColumns, DynamicGridColumnGroupings groupings)
  1086. {
  1087. // Yo, please don't remove this.
  1088. // The issue was when we were dynamically adding ActionColumns, and if we had to remove and then re-add them, we were getting massive performance hits
  1089. // for no reason. I think perhaps the image columns were trying to refer to data that didn't exist anymore when calling DataGrid.Columns.Refresh(),
  1090. // and thus some mega problems (perhaps even exceptions within Syncfusion) were occurring, and this seems to fix it.
  1091. // I don't pretend to know why it works; this is probably the strangest problem I've ever come across.
  1092. DataGrid.ItemsSource = null;
  1093. DataGrid.Columns.Suspend();
  1094. ColumnList.Clear();
  1095. DataGrid.Columns.Clear();
  1096. DataGrid.TableSummaryRows.Clear();
  1097. gridRowResizingOptions.ExcludeColumns = new List<string>();
  1098. ActionColumns = actionColumns.ToList();
  1099. Summaries.Clear();
  1100. LoadActionColumns(DynamicActionColumnPosition.Start);
  1101. LoadDataColumns(columns);
  1102. LoadActionColumns(DynamicActionColumnPosition.End);
  1103. LoadSummaries();
  1104. LoadStackedHeaders(groupings);
  1105. DataGrid.Columns.Resume();
  1106. DataGrid.RefreshColumns();
  1107. foreach (var key in _filterpredicates.Keys.ToArray())
  1108. if (DataGrid.Columns.Any(x => string.Equals(x.MappingName, key)))
  1109. {
  1110. var predicates = Serialization.Deserialize<List<FilterPredicate>>(_filterpredicates[key]);
  1111. foreach (var predicate in predicates)
  1112. {
  1113. DataGrid.Columns[key].FilterPredicates.Add(predicate);
  1114. DataGrid.Columns[key].FilteredFrom = FilteredFrom.FilterRow;
  1115. }
  1116. }
  1117. else
  1118. {
  1119. _filterpredicates.Remove(key);
  1120. }
  1121. ResizeColumns(DataGrid, DataGrid.ActualWidth - 2, DataGrid.ActualHeight - 2);
  1122. }
  1123. #endregion
  1124. #region Data
  1125. private bool _invalidating = false;
  1126. public void BeforeRefresh()
  1127. {
  1128. DataGrid.SelectionForegroundBrush = GetCellSelectionForegroundBrush();
  1129. DataGrid.RowSelectionBrush = GetCellSelectionBackgroundBrush();
  1130. }
  1131. public void RefreshData(CoreTable data)
  1132. {
  1133. var defaults = new List<object?>();
  1134. var result = new DataTable();
  1135. foreach (var column in data.Columns)
  1136. {
  1137. var colname = column.ColumnName.Replace('.', '_');
  1138. if (!result.Columns.Contains(colname))
  1139. {
  1140. result.Columns.Add(colname, column.DataType);
  1141. if (!Parent.IsDirectEditMode())
  1142. defaults.Add(column.DataType.GetDefault());
  1143. }
  1144. }
  1145. for (var i = 0; i < ActionColumns.Count; i++)
  1146. result.Columns.Add(string.Format("ActionColumn{0}", i),
  1147. ActionColumns[i] is DynamicImageColumn
  1148. ? typeof(BitmapImage)
  1149. : typeof(String)
  1150. );
  1151. foreach (var row in data.Rows)
  1152. {
  1153. var newrow = result.NewRow();
  1154. CoreRowToDataRow(newrow, row, defaults);
  1155. result.Rows.Add(newrow);
  1156. }
  1157. result.ColumnChanged += Result_ColumnChanged;
  1158. //int rowIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex;
  1159. //int columnIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
  1160. //int scrollRowIndex = DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex;
  1161. DataGrid.ItemsSource = result;
  1162. //this.DataGrid.ScrollInView(new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(scrollRowIndex, columnIndex));
  1163. ResizeColumns(DataGrid, DataGrid.ActualWidth - 1, DataGrid.ActualHeight);
  1164. UpdateRecordCount();
  1165. }
  1166. public void InvalidateRow(CoreRow row)
  1167. {
  1168. var table = DataGridItems;
  1169. if(table is null)
  1170. {
  1171. return;
  1172. }
  1173. _invalidating = true;
  1174. var rowdata = new List<object?>(row.Values);
  1175. foreach (var ac in ActionColumns)
  1176. rowdata.Add(ac.Data(row));
  1177. var datarow = DataGridItems.Rows[row.Index];
  1178. for (var i = 0; i < rowdata.Count; i++)
  1179. datarow[i] = rowdata[i] ?? DBNull.Value;
  1180. _invalidating = false;
  1181. //datarow.ItemArray = rowdata.ToArray();
  1182. }
  1183. private void CoreRowToDataRow(DataRow newrow, CoreRow row, List<object?> defaults)
  1184. {
  1185. var rowdata = new List<object?>(row.Values);
  1186. foreach (var ac in ActionColumns)
  1187. rowdata.Add(ac.Data(row));
  1188. try
  1189. {
  1190. var data = ProcessRow(rowdata, defaults).ToArray();
  1191. newrow.ItemArray = data;
  1192. }
  1193. catch (Exception)
  1194. {
  1195. throw;
  1196. }
  1197. }
  1198. private static IEnumerable<object?> ProcessRow(List<object?> values, List<object?> defaults)
  1199. {
  1200. if (defaults == null || !defaults.Any())
  1201. return values;
  1202. var result = new List<object?>();
  1203. for (var i = 0; i < values.Count; i++)
  1204. {
  1205. var value = values[i];
  1206. var defaultvalue = i < defaults.Count ? defaults[i] : null;
  1207. result.Add(value == null || (value.Equals(defaultvalue) && !value.GetType().IsEnum) ? null : value);
  1208. }
  1209. return result;
  1210. }
  1211. #endregion
  1212. #region Direct Edit
  1213. private bool bChanged;
  1214. private class DirectEditingObject
  1215. {
  1216. public T Object { get; set; }
  1217. public CoreRow Row { get; set; }
  1218. public DataRow? DataRow { get; set; }
  1219. public DirectEditingObject(T obj, CoreRow row, DataRow? dataRow)
  1220. {
  1221. Object = obj;
  1222. Row = row;
  1223. DataRow = dataRow;
  1224. }
  1225. }
  1226. private DirectEditingObject? _editingObject;
  1227. private DirectEditingObject EnsureEditingObject(CoreRow row)
  1228. {
  1229. _editingObject ??= new(Parent.LoadItem(row), row, DataGridItems?.Rows[row.Index]);
  1230. return _editingObject;
  1231. }
  1232. private DataRow? GetDataRow(CoreRow row)
  1233. {
  1234. return DataGridItems?.Rows[row.Index];
  1235. }
  1236. void IDynamicGridUIComponent<T>.UpdateCell(CoreRow row, string column, object? value)
  1237. {
  1238. var dataRow = GetDataRow(row);
  1239. var datacolname = column.Replace(".", "_");
  1240. if(dataRow is not null)
  1241. {
  1242. dataRow[datacolname] = value ?? DBNull.Value;
  1243. }
  1244. }
  1245. void IDynamicGridUIComponent<T>.UpdateRow(CoreRow row)
  1246. {
  1247. var dataRow = GetDataRow(row);
  1248. if(dataRow is not null)
  1249. {
  1250. foreach(var (key, value) in row)
  1251. {
  1252. var datacolname = key.Replace(".", "_");
  1253. var dataValue = dataRow[datacolname];
  1254. if (!Equals(dataValue, value) && !(value is null && dataValue == DBNull.Value))
  1255. {
  1256. dataRow[datacolname] = value ?? DBNull.Value;
  1257. }
  1258. }
  1259. for (var i = 0; i < ActionColumns.Count; i++)
  1260. dataRow[$"ActionColumn{i}"] = ActionColumns[i].Data(row);
  1261. }
  1262. }
  1263. private void DoEntityChanged(IDynamicColumnBase column, DynamicColumnEntityChangedEventArgs args)
  1264. {
  1265. if (_editingObject is null) return;
  1266. Parent.EntityChanged(_editingObject.Object, _editingObject.Row, args.ColumnName, args.Changes);
  1267. }
  1268. private void UpdateData(string column, Dictionary<CoreColumn, object?> updates)
  1269. {
  1270. if (_editingObject is null)
  1271. return;
  1272. var coreRow = _editingObject.Row;
  1273. Parent.UpdateData(_editingObject.Object, coreRow, column, updates);
  1274. }
  1275. private void UpdateData(int rowIndex, int columnIndex)
  1276. {
  1277. var table = DataGridItems;
  1278. if (table is null)
  1279. return;
  1280. if (GetColumn(columnIndex) is DynamicGridColumn gridcol)
  1281. {
  1282. var datacol = Parent.Data.Columns.FirstOrDefault(x => x.ColumnName.Equals(gridcol.ColumnName));
  1283. if (datacol != null)
  1284. {
  1285. var datacolindex = Parent.Data.Columns.IndexOf(datacol);
  1286. var value = table.Rows[rowIndex][datacolindex];
  1287. if (value is DBNull)
  1288. value = CoreUtils.GetDefault(datacol.DataType);
  1289. UpdateData(datacol.ColumnName, new Dictionary<CoreColumn, object?>() { { datacol, value } });
  1290. }
  1291. }
  1292. }
  1293. private void DataGrid_CurrentCellBeginEdit(object? sender, CurrentCellBeginEditEventArgs e)
  1294. {
  1295. var table = DataGridItems;
  1296. var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
  1297. if (table is null || row is null)
  1298. return;
  1299. EnsureEditingObject(row);
  1300. var column = DataGrid.Columns[e.RowColumnIndex.ColumnIndex] as GridComboBoxColumn;
  1301. if (column != null && column.ItemsSource == null)
  1302. {
  1303. var colname = column.MappingName;
  1304. var colno = table.Columns.IndexOf(colname);
  1305. var property = Parent.Data.Columns[colno].ColumnName;
  1306. var prop = CoreUtils.GetProperty(typeof(T), property);
  1307. var editor = prop.GetEditor();
  1308. if (editor is ILookupEditor lookupEditor)
  1309. {
  1310. if (!Lookups.ContainsKey(property))
  1311. Lookups[property] = lookupEditor.Values(typeof(T), property);
  1312. var combo = column;
  1313. combo.ItemsSource = Lookups[property].ToDictionary(Lookups[property].Columns[0].ColumnName, "Display");
  1314. combo.SelectedValuePath = "Key";
  1315. combo.DisplayMemberPath = "Value";
  1316. }
  1317. }
  1318. bChanged = false;
  1319. }
  1320. private void Result_ColumnChanged(object sender, DataColumnChangeEventArgs e)
  1321. {
  1322. if (_invalidating) return;
  1323. if (sender is not DataTable table) return;
  1324. var rowIdx = table.Rows.IndexOf(e.Row);
  1325. if (rowIdx < 0)
  1326. return;
  1327. var row = Parent.Data.Rows[rowIdx];
  1328. var colIdx = table.Columns.IndexOf(e.Column);
  1329. if (colIdx < 0 || colIdx >= Parent.Data.Columns.Count)
  1330. return;
  1331. var data = Parent.Data;
  1332. var dataCol = Parent.Data.Columns[colIdx];
  1333. var col = ColumnList.OfType<DynamicGridColumn>()
  1334. .FirstOrDefault(x => x.ColumnName.Equals(dataCol.ColumnName));
  1335. if (col is null)
  1336. return;
  1337. if (col is DynamicGridCheckBoxColumn<T>)
  1338. {
  1339. EnsureEditingObject(row);
  1340. if(_editingObject is not null)
  1341. {
  1342. var value = e.Row[e.Column!];
  1343. if (value is DBNull)
  1344. value = CoreUtils.GetDefault(dataCol.DataType);
  1345. _invalidating = true;
  1346. UpdateData(dataCol.ColumnName, new Dictionary<CoreColumn, object?>() { { dataCol, value } });
  1347. _invalidating = false;
  1348. }
  1349. _editingObject = null;
  1350. }
  1351. if (_editingObject is not null)
  1352. bChanged = true;
  1353. }
  1354. private void DataGrid_CurrentCellDropDownSelectionChanged(object? sender,
  1355. CurrentCellDropDownSelectionChangedEventArgs e)
  1356. {
  1357. var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
  1358. if (row is null)
  1359. return;
  1360. EnsureEditingObject(row);
  1361. if ((_editingObject is not null) && (e.SelectedItem is Tuple<object?, string> tuple))
  1362. {
  1363. var mappedname = DataGrid.Columns[e.RowColumnIndex.ColumnIndex].MappingName;
  1364. var colno = DataGridItems.Columns.IndexOf(mappedname);
  1365. var corecol = Parent.Data.Columns[colno].ColumnName;
  1366. var updates = new Dictionary<CoreColumn, object?>();
  1367. var prefix = String.Join(".", corecol.Split(".").Reverse().Skip(1).Reverse());
  1368. var field = corecol.Split(".").Last();
  1369. var prop = CoreUtils.GetProperty(typeof(T), corecol);
  1370. if (prop.GetEditor() is ILookupEditor editor)
  1371. {
  1372. var data = editor.Values(typeof(T), corecol);
  1373. var lookuprow = data.Rows.FirstOrDefault(r => Equals(r[field], tuple.Item1))
  1374. ?? data.NewRow(true);
  1375. foreach (CoreColumn lookupcol in data.Columns)
  1376. {
  1377. var columnname = String.IsNullOrWhiteSpace(prefix)
  1378. ? lookupcol.ColumnName
  1379. : String.Join(".", prefix, lookupcol.ColumnName);
  1380. var updatecol = Parent.Data.Columns.FirstOrDefault(x => String.Equals(x.ColumnName, columnname));
  1381. if (updatecol != null)
  1382. updates[updatecol] = lookuprow[lookupcol.ColumnName];
  1383. }
  1384. UpdateData(corecol, updates);
  1385. bChanged = true;
  1386. }
  1387. }
  1388. }
  1389. private void DataGrid_CurrentCellEndEdit(object? sender, CurrentCellEndEditEventArgs e)
  1390. {
  1391. if (_editingObject is not null && bChanged)
  1392. {
  1393. UpdateData(_editingObject.Row.Index, e.RowColumnIndex.ColumnIndex);
  1394. }
  1395. if (bChanged)
  1396. Parent.DoChanged();
  1397. bChanged = false;
  1398. _editingObject = null;
  1399. // Commented out on 19/02/2024 by Kenric. I don't see this being necessary, though I could be wrong. Nevertheless, it was causing a bug when
  1400. // editing the filter row. It seems that this causes Syncfusion to commit the filter predicates internally, which means that after leaving a
  1401. // filter row cell, the filter remained even once it was cleared, meaning a refresh was necessary to get the data back.
  1402. // I've tested on Bills to see if editing works with this empty, and it seems so.
  1403. //DataGridItems?.AcceptChanges();
  1404. }
  1405. private void DataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
  1406. {
  1407. if (e.Key == Key.OemPeriod)
  1408. {
  1409. var editor = e.OriginalSource as TimeSpanEdit;
  1410. if (editor != null && editor.SelectionStart < 2) editor.SelectionStart = 3;
  1411. }
  1412. else if (e.Key == Key.Tab)
  1413. {
  1414. if (Parent.IsDirectEditMode())
  1415. {
  1416. DataGrid.SelectionController.CurrentCellManager.EndEdit();
  1417. DataGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
  1418. DataGrid.SelectionController.CurrentCellManager.BeginEdit();
  1419. e.Handled = true;
  1420. }
  1421. }
  1422. }
  1423. #endregion
  1424. #region Drag + Drop
  1425. private void DataGrid_DragOver(object sender, DragEventArgs e)
  1426. {
  1427. Parent.DragOver(sender, e);
  1428. }
  1429. private void DataGrid_Drop(object sender, DragEventArgs e)
  1430. {
  1431. Parent.Drop(sender, e);
  1432. }
  1433. private void RowDragDropController_DragStart(object? sender, GridRowDragStartEventArgs e)
  1434. {
  1435. var rows = e.DraggingRecords.Select(record =>
  1436. {
  1437. var rowIndex = DataGrid.ResolveToRowIndex(record);
  1438. return GetRowFromIndex(rowIndex);
  1439. }).NotNull().ToArray();
  1440. Parent.DragStart(sender, rows);
  1441. }
  1442. #endregion
  1443. }