DynamicGridGridUIComponent.cs 66 KB

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