DynamicGridGridUIComponent.cs 64 KB

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