DynamicGridGridUIComponent.cs 64 KB

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