123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908 |
- using InABox.Clients;
- using InABox.Core;
- using InABox.Scripting;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.Xaml.Behaviors;
- using org.omg.PortableInterceptor;
- using sun.reflect.generics.tree;
- using sun.util.resources.cldr.fr;
- using Syncfusion.Data;
- using Syncfusion.Data.Extensions;
- using Syncfusion.UI.Xaml.Grid;
- using Syncfusion.UI.Xaml.Grid.Cells;
- using Syncfusion.UI.Xaml.Grid.Helpers;
- using Syncfusion.UI.Xaml.ScrollAxis;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Data;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Effects;
- using System.Windows.Media.Imaging;
- using System.Windows.Threading;
- using DataColumn = System.Data.DataColumn;
- using DataRow = System.Data.DataRow;
- using TimeSpanEdit = Syncfusion.Windows.Shared.TimeSpanEdit;
- namespace InABox.DynamicGrid;
- public enum DynamicGridLines
- {
- Both,
- Horizontal,
- Vertical,
- None
- }
- public class DynamicGridGridUIComponent<T> : IDynamicGridUIComponent<T>, IDynamicGridGridUIComponent<T>
- where T : BaseObject, new()
- {
- private readonly Dictionary<string, string> _filterpredicates = new();
-
- private Dictionary<string, CoreTable> Lookups = new();
- private IDynamicGridUIComponentParent<T> _parent;
- public IDynamicGridUIComponentParent<T> Parent
- {
- get => _parent;
- set
- {
- _parent = value;
- CellBackgroundConverter = new DynamicGridGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellBackground);
- CellForegroundConverter = new DynamicGridGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellForeground);
- CellFontSizeConverter = new DynamicGridGridCellStyleConverter<double?>(Parent, GetCellFontSize);
- CellFontStyleConverter = new DynamicGridGridCellStyleConverter<System.Windows.FontStyle?>(Parent, GetCellFontStyle);
- CellFontWeightConverter = new DynamicGridGridCellStyleConverter<System.Windows.FontWeight?>(Parent, GetCellFontWeight);
- DataGrid.RowStyleSelector = Parent.RowStyleSelector;
- DataGrid.TableSummaryRowStyleSelector = new SummaryRowStyleSelector(this, GetSummaryRowStyle);
- DataGrid.TableSummaryCellStyleSelector = new SummaryCellStyleSelector(this, GetSummaryCellStyle);
- }
- }
- FrameworkElement IDynamicGridUIComponent<T>.Control => DataGrid;
- #region IDynamicGridGridUIComponent
- IList<DynamicColumnBase> IDynamicGridGridUIComponent<T>.ColumnList => ColumnList;
- int IDynamicGridGridUIComponent<T>.RowHeight => (int)RowHeight;
- #endregion
- protected readonly SfDataGrid DataGrid;
- private readonly ContextMenu ColumnsMenu;
- private readonly GridRowSizingOptions gridRowResizingOptions = new() { CanIncludeHiddenColumns = false, AutoFitMode = AutoFitMode.Default };
- /// <summary>
- /// <see langword="null"/> when <see cref="DataGrid.ItemsSource"/> is <see langword="null"/>, generally while the grid is refreshing its columns.
- /// </summary>
- private DataTable? DataGridItems => (DataGrid.ItemsSource as DataTable);
- private DynamicGridGridCellStyleConverter<System.Windows.Media.Brush?> CellBackgroundConverter;
- private DynamicGridGridCellStyleConverter<System.Windows.Media.Brush?> CellForegroundConverter;
- private DynamicGridGridCellStyleConverter<double?> CellFontSizeConverter;
- private DynamicGridGridCellStyleConverter<System.Windows.FontStyle?> CellFontStyleConverter;
- private DynamicGridGridCellStyleConverter<System.Windows.FontWeight?> CellFontWeightConverter;
- #region Configuration
- private DynamicGridLines _gridLines = DynamicGridLines.Both;
- public DynamicGridLines GridLines
- {
- get => _gridLines;
- set
- {
- _gridLines = value;
- DataGrid.GridLinesVisibility = value switch
- {
- DynamicGridLines.Both => GridLinesVisibility.Both,
- DynamicGridLines.Vertical => GridLinesVisibility.Vertical,
- DynamicGridLines.Horizontal => GridLinesVisibility.Horizontal,
- _ => GridLinesVisibility.None,
- };
- }
- }
- public double RowHeight
- {
- get => DataGrid.RowHeight;
- set => DataGrid.RowHeight = value;
- }
- public double HeaderRowHeight
- {
- get => DataGrid.HeaderRowHeight;
- set => DataGrid.HeaderRowHeight = value;
- }
- #endregion
- public DynamicGridGridUIComponent()
- {
- ColumnsMenu = new ContextMenu();
- ColumnsMenu.Opened += ColumnsMenu_ContextMenuOpening;
- DataGrid = new SfDataGrid();
- DataGrid.VerticalAlignment = VerticalAlignment.Stretch;
- DataGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
- DataGrid.HeaderContextMenu = ColumnsMenu;
- DataGrid.CellTapped += DataGrid_CellTapped;
- DataGrid.CellDoubleTapped += DataGrid_CellDoubleTapped;
- DataGrid.SelectionChanging += DataGrid_SelectionChanging;
- DataGrid.SelectionChanged += DataGrid_SelectionChanged;
- DataGrid.SelectionMode = GridSelectionMode.Extended;
- DataGrid.SelectionUnit = GridSelectionUnit.Row;
- DataGrid.CanMaintainScrollPosition = true;
- DataGrid.SummaryCalculationUnit = SummaryCalculationUnit.AllRows;
- DataGrid.LiveDataUpdateMode = LiveDataUpdateMode.AllowSummaryUpdate;
- DataGrid.NavigationMode = NavigationMode.Row;
- DataGrid.AllowEditing = false;
- DataGrid.EditTrigger = EditTrigger.OnTap;
- DataGrid.CurrentCellBeginEdit += DataGrid_CurrentCellBeginEdit;
- DataGrid.CurrentCellEndEdit += DataGrid_CurrentCellEndEdit;
- DataGrid.CurrentCellDropDownSelectionChanged += DataGrid_CurrentCellDropDownSelectionChanged;
- DataGrid.PreviewKeyUp += DataGrid_PreviewKeyUp;
- DataGrid.BorderBrush = new SolidColorBrush(Colors.Gray);
- DataGrid.BorderThickness = new Thickness(0.75F);
- DataGrid.Background = new SolidColorBrush(Colors.DimGray);
- DataGrid.AutoGenerateColumns = false;
- DataGrid.ColumnSizer = GridLengthUnitType.AutoLastColumnFill;
- DataGrid.SelectionForegroundBrush = GetCellSelectionForegroundBrush() ?? DynamicGridUtils.SelectionForeground;
- DataGrid.RowSelectionBrush = GetCellSelectionBackgroundBrush() ?? DynamicGridUtils.SelectionBackground;
-
- DataGrid.AllowDraggingRows = false;
- DataGrid.Drop += DataGrid_Drop;
- DataGrid.DragOver += DataGrid_DragOver;
- DataGrid.RowDragDropTemplate = TemplateGenerator.CreateDataTemplate(() =>
- {
- // var border = new Border();
- // border.Width = 100;
- // border.Height = 100;
- // border.BorderBrush = new SolidColorBrush(Colors.Firebrick);
- // border.Background = new SolidColorBrush(Colors.Red);
- // border.CornerRadius = new CornerRadius(5);
- // return border;
- return null;
- });
- DataGrid.RowDropIndicatorMode = DropIndicatorMode.Line;
- DataGrid.RowDragDropController ??= new GridRowDragDropController();
-
- DataGrid.CurrentCellBorderThickness = new Thickness(0);
- DataGrid.AllowFiltering = false;
- DataGrid.EnableDataVirtualization = true;
- DataGrid.RowHeight = 30;
- DataGrid.QueryRowHeight += DataGrid_QueryRowHeight;
- DataGrid.HeaderRowHeight = 30;
- DataGrid.MouseLeftButtonUp += DataGrid_MouseLeftButtonUp;
- DataGrid.MouseRightButtonUp += DataGrid_MouseRightButtonUp;
- DataGrid.KeyUp += DataGrid_KeyUp;
- DataGrid.PreviewGotKeyboardFocus += DataGrid_PreviewGotKeyboardFocus;
- DataGrid.SelectionController = new GridSelectionControllerExt(DataGrid, this);
-
- DataGrid.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Visible);
-
- DataGrid.FilterChanging += DataGrid_FilterChanging;
- DataGrid.FilterChanged += DataGrid_FilterChanged;
- var fltstyle = new Style(typeof(GridFilterControl));
- fltstyle.Setters.Add(new Setter(GridFilterControl.FilterModeProperty, FilterMode.Both));
- fltstyle.Setters.Add(new Setter(GridFilterControl.SortOptionVisibilityProperty, Visibility.Collapsed));
- DataGrid.FilterPopupStyle = fltstyle;
- DataGrid.CellToolTipOpening += DataGrid_CellToolTipOpening;
- DataGrid.SizeChanged += DataGrid_SizeChanged;
- }
- public class GridSelectionControllerExt(SfDataGrid datagrid, DynamicGridGridUIComponent<T> grid) : GridSelectionController(datagrid)
- {
- private DynamicGridGridUIComponent<T> Grid = grid;
- public override bool HandleKeyDown(KeyEventArgs args)
- {
- if (args.Key == Key.Escape)
- {
- Grid.CancelEdit();
- return false;
- }
- else
- {
- return base.HandleKeyDown(args);
- }
- }
- }
- private void ChangeValue(object oldValue, object newValue)
- {
- }
- private void ColumnsMenu_ContextMenuOpening(object sender, RoutedEventArgs e)
- {
- if (sender is not ContextMenu menu) return;
- menu.Items.Clear();
- Parent.LoadColumnsMenu(menu);
- }
- #region Selection
- public CoreRow[] SelectedRows
- {
- get => GetSelectedRows();
- set => SetSelectedRows(value);
- }
- private CoreRow[] GetSelectedRows()
- {
- var result = new List<CoreRow>();
- foreach (var item in DataGrid.SelectedItems)
- {
- if (item is DataRowView datarow && GetRow(datarow.Row) is CoreRow row)
- {
- result.Add(row);
- }
- }
- return result.ToArray();
- }
- private void SetSelectedRows(CoreRow[] rows)
- {
- DataGrid.SelectedItems.Clear();
- var table = DataGridItems;
- if(table is null) return;
- var dataRows = rows.Where(x => x.Index > -1).Select(row =>
- {
- return table.Rows[row.Index];
- });
- if (!Parent.Options.MultiSelect)
- {
- dataRows = dataRows.Take(1);
- }
- foreach (var row in dataRows)
- {
- var record = DataGrid.View?.Records.FirstOrDefault(x => (x.Data as DataRowView)!.Row == row);
- if(record?.Data is DataRowView rowView)
- {
- DataGrid.SelectedItems.Add(rowView);
- }
- }
- }
- private void DataGrid_SelectionChanging(object? sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangingEventArgs e)
- {
- var cancel = new CancelEventArgs();
- Parent.BeforeSelection(cancel);
- if (cancel.Cancel)
- {
- e.Cancel = true;
- }
- }
- private void DataGrid_SelectionChanged(object? sender, GridSelectionChangedEventArgs e)
- {
- if(Parent.IsReady && !Parent.IsRefreshing)
- {
- StartTimer();
- }
- }
- private DispatcherTimer? clicktimer;
- private void StartTimer()
- {
- if (clicktimer is null)
- {
- clicktimer = new DispatcherTimer();
- clicktimer.Interval = TimeSpan.FromMilliseconds(200);
- clicktimer.Tick += (o, e) =>
- {
- clicktimer.IsEnabled = false;
- Parent.SelectItems(SelectedRows);
- };
- }
- clicktimer.IsEnabled = true;
- }
- private void StopTimer()
- {
- if (clicktimer is not null)
- clicktimer.IsEnabled = false;
- }
- #endregion
- public CoreRow[] GetVisibleRows()
- {
- var items = DataGrid.ItemsSource;
- var result = new List<CoreRow>();
- var table = DataGridItems;
- if (table is null) return Array.Empty<CoreRow>();
- var rows = DataGrid.View.Records.Select(x => (x.Data as DataRowView)!).ToList();
- foreach (var row in rows)
- {
- if(GetRow(row.Row) is CoreRow coreRow)
- {
- result.Add(coreRow);
- }
- }
- return result.ToArray();
- }
- private bool bFilterVisible;
- private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!DataGrid.IsEnabled)
- return;
- var visualContainer = DataGrid.GetVisualContainer();
- var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
- var columnindex = DataGrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex);
- var column = GetColumn(columnindex);
- if(column is not null)
- {
- Parent.OpenColumnMenu(column);
- }
- }
- private void DataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- if (!DataGrid.IsEnabled)
- return;
- var visualContainer = DataGrid.GetVisualContainer();
- var rowcolumnindex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
- var columnindex = DataGrid.ResolveToGridVisibleColumnIndex(rowcolumnindex.ColumnIndex);
- // Header Click Here!
- if (rowcolumnindex.RowIndex < DataGrid.StackedHeaderRows.Count + 1)
- {
- var column = GetColumn(columnindex);
- if(column is DynamicActionColumn dac)
- {
- Parent.ExecuteActionColumn(dac, null);
- }
- }
- else if (!bFilterVisible)
- {
- StartTimer();
- }
- }
- private void DataGrid_CellTapped(object? sender, GridCellTappedEventArgs e)
- {
- if (!DataGrid.IsEnabled)
- return;
- if (GetColumn(e.RowColumnIndex.ColumnIndex) is DynamicActionColumn dac)
- {
- if(e.ChangedButton == MouseButton.Left || (e.ChangedButton == MouseButton.Right && dac is DynamicMenuColumn))
- {
- Parent.ExecuteActionColumn(dac, SelectedRows);
- }
- }
- else
- {
- StartTimer();
- }
- }
- private void DataGrid_KeyUp(object sender, KeyEventArgs e)
- {
- if (sender != DataGrid) return;
- Parent.HandleKey(e);
- }
- private void DataGrid_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
- {
- var bOld = bFilterVisible;
- if (e.NewFocus is GridFilterControl)
- bFilterVisible = true;
- else if (e.NewFocus is ScrollViewer || e.NewFocus is SfDataGrid)
- bFilterVisible = false;
- if (bOld && !bFilterVisible)
- {
- Parent.SelectItems(SelectedRows);
- }
- }
- // Please always use this function where possible!
- /// <summary>
- /// Get the <see cref="CoreRow"/> associated with a <paramref name="rowIndex"/> given from the Syncfusion DataGrid.
- /// </summary>
- /// <remarks>
- /// This is mandatory to use whenever we want the data associated with an index which Syncfusion has given us,
- /// because filtering and sorting will cause normal indexing operations to fail.
- /// </remarks>
- /// <param name="rowIndex"></param>
- /// <returns></returns>
- private CoreRow? GetRowFromIndex(int rowIndex)
- {
- // Syncfusion has given us the row index, so it also will give us the correct row, after sorting.
- // Hence, here we use the syncfusion DataGrid.GetRecordAtRowIndex, which *should* always return a DataRowView.
- var row = DataGrid.GetRecordAtRowIndex(rowIndex);
- if (row is not DataRowView dataRowView || DataGridItems is not DataTable table)
- return null;
- return GetRow(dataRowView.Row);
- }
- private CoreRow? GetRow(DataRow row)
- {
- return _rowMap.GetValueOrDefault(row);
- }
- private void DataGrid_CellDoubleTapped(object? sender, GridCellDoubleTappedEventArgs e)
- {
- StopTimer();
- Parent.DoubleClickCell(GetRowFromIndex(e.RowColumnIndex.RowIndex), GetColumn(e.RowColumnIndex.ColumnIndex));
- }
-
- private void DataGrid_QueryRowHeight(object? sender, QueryRowHeightEventArgs e)
- {
- if (e.RowIndex > 0)
- {
- e.Height = DataGrid.RowHeight;
- if (DataGrid.GridColumnSizer.GetAutoRowHeight(e.RowIndex, gridRowResizingOptions, out var autoHeight))
- if (autoHeight > DataGrid.RowHeight)
- e.Height = autoHeight;
- e.Handled = true;
- }
- }
- private void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- if (Parent.IsReady && !Parent.IsRefreshing)
- ResizeColumns(DataGrid, e.NewSize.Width - 2, e.NewSize.Height - 2);
- }
- public bool OptionsChanged()
- {
- ColumnsMenu.Visibility = Parent.Options.SelectColumns ? Visibility.Visible : Visibility.Hidden;
- var allowEditing = Parent.IsDirectEditMode();
- var reloadColumns = false;
- if (DataGrid.AllowEditing != allowEditing)
- {
- DataGrid.NavigationMode = allowEditing ? NavigationMode.Cell : NavigationMode.Row;
- DataGrid.AllowEditing = allowEditing;
- reloadColumns = true;
- }
- DataGrid.AllowFiltering = Parent.Options.FilterRows && Parent.CanFilter();
- DataGrid.FilterRowPosition = Parent.Options.FilterRows && Parent.CanFilter() ? FilterRowPosition.FixedTop : FilterRowPosition.None;
- if (Parent.Options.DragSource || Parent.Options.ReorderRows)
- {
- if (!DataGrid.AllowDraggingRows)
- {
- DataGrid.AllowDraggingRows = true;
- DataGrid.RowDragDropController.DragStart += RowDragDropController_DragStart;
- DataGrid.RowDragDropController.DragOver += RowDragDropController_DragOver;
- }
- }
- else
- {
- if (DataGrid.AllowDraggingRows)
- {
- DataGrid.AllowDraggingRows = false;
- DataGrid.RowDragDropController.DragStart -= RowDragDropController_DragStart;
- DataGrid.RowDragDropController.DragOver -= RowDragDropController_DragOver;
- }
- }
- if (Parent.Options.DragTarget || Parent.Options.ReorderRows)
- {
- if (!DataGrid.AllowDrop)
- {
- DataGrid.AllowDrop = true;
- DataGrid.RowDragDropController.Drop += RowDragDropController_Drop;
- DataGrid.RowDragDropController.Dropped += RowDragDropController_Dropped;
- }
- }
- else
- {
- if (DataGrid.AllowDrop)
- {
- DataGrid.AllowDrop = false;
- DataGrid.RowDragDropController.Drop -= RowDragDropController_Drop;
- DataGrid.RowDragDropController.Dropped -= RowDragDropController_Dropped;
- }
- }
-
- DataGrid.SelectionMode = Parent.Options.MultiSelect ? GridSelectionMode.Extended : GridSelectionMode.Single;
- return reloadColumns && DataGrid.Columns.Count > 0;
- }
- private void DataGrid_FilterChanging(object? sender, GridFilterEventArgs e)
- {
- var col = GetColumn(DataGrid.Columns.IndexOf(e.Column)) as DynamicActionColumn;
- if (col?.GetFilterExpression != null)
- {
- col.SetFilters(e.FilterPredicates);
- var view = (DataGrid.ItemsSource as DataTable)?.DefaultView;
- view.ApplyFilters(ColumnList.OfType<DynamicActionColumn>());
- e.Handled = true;
- }
- }
-
- private void DataGrid_FilterChanged(object? o, GridFilterEventArgs e)
- {
- var col = DataGrid.Columns.IndexOf(e.Column);
- if (GetColumn(col) is DynamicActionColumn column)
- {
- column.SetFilters(e.FilterPredicates);
- e.Handled = true;
- }
- if (e.FilterPredicates == null)
- {
- if (_filterpredicates.ContainsKey(e.Column.MappingName))
- _filterpredicates.Remove(e.Column.MappingName);
- }
- else
- {
- _filterpredicates[e.Column.MappingName] = Serialization.Serialize(e.FilterPredicates, true);
- }
- Parent.UIFilterChanged(this);
- UpdateRecordCount();
- }
- private void UpdateRecordCount()
- {
- var count = DataGrid.View != null ? DataGrid.View.Records.Count : Parent.Data.Rows.Count;
- Parent.UpdateRecordCount(count);
- }
- private void DataGrid_CellToolTipOpening(object? sender, GridCellToolTipOpeningEventArgs e)
- {
- var column = GetColumn(e.RowColumnIndex.ColumnIndex);
- if (column is DynamicActionColumn ac)
- {
- var toolTip = ac.ToolTip;
- if (toolTip is null)
- return;
- var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
- e.ToolTip.Template = TemplateGenerator.CreateControlTemplate(
- typeof(ToolTip),
- () => toolTip.Invoke(ac, row)
- );
- }
- else if(column is DynamicGridColumn gc)
- {
- e.ToolTip.Content = gc.Editor.ToolTip;
- }
- }
- public void AddVisualFilter(string column, string value, FilterType filtertype = FilterType.Contains)
- {
- if (string.IsNullOrWhiteSpace(value))
- return;
- var col = DataGrid.Columns.FirstOrDefault((x=>String.Equals(x.MappingName?.ToUpper(),column?.Replace(".", "_").ToUpper())));
- if (col != null)
- {
- col.FilterPredicates.Add(new FilterPredicate { FilterType = filtertype, FilterValue = value });
- col.FilteredFrom = FilteredFrom.FilterRow;
- }
- }
- public List<Tuple<string, Func<CoreRow, bool>>> GetFilterPredicates()
- {
- var list = new List<Tuple<string, Func<CoreRow, bool>>>();
- foreach (var column in DataGrid.Columns)
- {
- var colIndex = DataGrid.Columns.IndexOf(column);
- var col = ColumnList[colIndex];
- if (col is DynamicGridColumn gridColumn)
- {
- var rowPredicate = DynamicGridGridUIComponentExtension.ConvertColumnPredicates(gridColumn, column.FilterPredicates);
- if(rowPredicate is not null)
- {
- list.Add(new(gridColumn.ColumnName, rowPredicate));
- }
- }
- else if(col is DynamicActionColumn dac && dac.FilterRecord is not null)
- {
- if(dac.SelectedFilters is not null && dac.SelectedFilters.Length > 0)
- {
- list.Add(new(column.MappingName, (row) => dac.FilterRecord(row, dac.SelectedFilters)));
- }
- if(dac.ExcludeFilters is not null && dac.ExcludeFilters.Length > 0)
- {
- list.Add(new(column.MappingName, (row) => !dac.FilterRecord(row, dac.ExcludeFilters)));
- }
- }
- }
- return list;
- }
- public void ScrollIntoView(CoreRow row)
- {
- var rowIdx = DataGrid.ResolveToRowIndex(row.Index + 1);
- DataGrid.ScrollInView(new RowColumnIndex(rowIdx, 0));
- }
- #region Styles
- protected virtual Brush? GetCellSelectionForegroundBrush() => DynamicGridUtils.SelectionForeground;
- protected virtual Brush? GetCellSelectionBackgroundBrush() => DynamicGridUtils.SelectionBackground;
- protected virtual Brush? GetCellBackground(CoreRow row, DynamicColumnBase column) => null;
- protected virtual Brush? GetCellForeground(CoreRow row, DynamicColumnBase column) => null;
- protected virtual double? GetCellFontSize(CoreRow row, DynamicColumnBase column) => null;
- protected virtual FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column) => null;
- protected virtual FontWeight? GetCellFontWeight(CoreRow row, DynamicColumnBase column) => null;
- protected virtual Style GetCellStyle(DynamicColumnBase column)
- {
- var style = new Style(typeof(GridCell));
- return style;
- }
- protected virtual Style GetHeaderCellStyle(DynamicColumnBase column)
- {
- var headStyle = new Style(typeof(GridHeaderCellControl));
- headStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headStyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headStyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
- if (column is DynamicActionColumn actionColumn)
- {
- headStyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0)));
- headStyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
- if (column is DynamicImageColumn imgCol)
- {
- if (imgCol.HeaderText.IsNullOrWhiteSpace())
- {
- var image = imgCol.Image?.Invoke(null);
- if (image != null)
- {
- headStyle.AddSetter(GridHeaderCellControl.PaddingProperty, new Thickness(4.0));
- headStyle.AddSetter(GridHeaderCellControl.ContentTemplateProperty,
- TemplateGenerator.CreateDataTemplate(() =>
- {
- return new Image { Source = image };
- }));
- }
- }
- }
- if (actionColumn.VerticalHeader && !actionColumn.HeaderText.IsNullOrWhiteSpace())
- {
- headStyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
- headStyle.Setters.Add(new Setter(Control.TemplateProperty,
- Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
- }
- }
- return headStyle;
- }
- protected virtual Style GetColumnGroupHeaderCellStyle(DynamicGridColumnGroup group)
- {
- var headstyle = new Style(typeof(GridStackedHeaderCellControl));
- headstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headstyle.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
- headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.0, 0.0, 0, 0)));
- headstyle.Setters.Add(new Setter(Control.MarginProperty, new Thickness(0, 0, 1, 1)));
- return headstyle;
- }
- protected virtual Style GetSummaryRowStyle()
- {
- var style = new Style(typeof(TableSummaryRowControl));
- return style;
- }
- protected virtual Style GetSummaryCellStyle(DynamicColumnBase column)
- {
- var style = new Style(typeof(GridTableSummaryCell));
- style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- style.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
- if(column is DynamicGridColumn gridColumn)
- {
- style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty,
- column != null ? gridColumn.HorizontalAlignment(typeof(double)) : HorizontalAlignment.Right));
- }
- else if(column is DynamicTextColumn textColumn)
- {
- style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, textColumn.Alignment.HorizontalAlignment(typeof(string))));
- }
- else
- {
- style.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Right));
- }
- style.Setters.Add(new Setter(Control.BorderBrushProperty, new SolidColorBrush(Colors.Gray)));
- style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0, 0, 0.75, 0)));
- style.Setters.Add(new Setter(Control.FontSizeProperty, 12D));
- style.Setters.Add(new Setter(Control.FontWeightProperty, FontWeights.DemiBold));
- return style;
- }
- #endregion
- #region Columns
- private readonly List<DynamicColumnBase> ColumnList = new();
- private List<DynamicActionColumn> ActionColumns = new();
- private DynamicColumnBase? GetColumn(int index) =>
- index >= 0 && index < ColumnList.Count ? ColumnList[index] : null;
- private class StackedHeaderRenderer : GridStackedHeaderCellRenderer
- {
- private DynamicGridGridUIComponent<T> Grid;
- public StackedHeaderRenderer(DynamicGridGridUIComponent<T> grid)
- {
- Grid = grid;
- }
- public override void OnInitializeEditElement(DataColumnBase dataColumn, GridStackedHeaderCellControl uiElement, object dataContext)
- {
- if(dataContext is StackedColumn column && Grid.ColumnGroupMap.TryGetValue(column.MappingName, out var group))
- {
- uiElement.Style = Grid.GetColumnGroupHeaderCellStyle(group);
- }
- base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
- }
- }
- private Dictionary<string, DynamicGridColumnGroup> ColumnGroupMap = new();
- private void LoadStackedHeaders(DynamicGridColumnGroupings groupings)
- {
- DataGrid.StackedHeaderRows.Clear();
- ColumnGroupMap.Clear();
- var j = 0;
- foreach(var grouping in groupings)
- {
- var row = new StackedHeaderRow();
- var i = 0;
- foreach(var group in grouping.Groups)
- {
- var start = Math.Max(i, ColumnList.IndexOf(group.StartColumn));
- var end = Math.Max(start, ColumnList.IndexOf(group.EndColumn));
- if(end < start)
- {
- i = end + 1;
- continue;
- }
- var cols = Enumerable.Range(start, end - start + 1).Select(i => DataGrid.Columns[i]).ToArray();
- var name = $"_GroupColumn{j}";
- var stackedColumn = new StackedColumn
- {
- HeaderText = group.Header,
- ChildColumns = string.Join(',', cols.Select(x => x.MappingName)),
- MappingName = name
- };
- ColumnGroupMap.Add(name, group);
- j = j + 1;
- row.StackedColumns.Add(stackedColumn);
- i = end + 1;
- }
- DataGrid.StackedHeaderRows.Add(row);
- }
- if(groupings.Count > 0)
- {
- DataGrid.CellRenderers.Remove("StackedHeader");
- DataGrid.CellRenderers.Add("StackedHeader", new StackedHeaderRenderer(this));
- }
- }
- int IDynamicGridUIComponent<T>.DesiredWidth()
- {
- return this.DesiredWidth();
- }
- private void ResizeColumns(SfDataGrid grid, double width, double height)
- {
- if (Parent.Data == null || width <= 0)
- return;
- grid.Dispatcher.BeginInvoke(() =>
- {
- foreach (var (index, size) in this.CalculateColumnSizes(width))
- DataGrid.Columns[index].Width = Math.Max(0.0F, size);
- });
- }
- private ObservableCollection<ISummaryColumn> Summaries = new();
- private void AddCellStyleConverters(Style cellstyle, DynamicColumnBase column, string sColName)
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- WPFUtils.CreateMultiBinding(
- CellBackgroundConverter.WrapConverter(x => x[0]),
- parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName))));
- cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
- WPFUtils.CreateMultiBinding(
- CellForegroundConverter.WrapConverter(x => x[0]),
- parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName))));
- cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
- WPFUtils.CreateMultiBinding(
- CellFontSizeConverter.WrapConverter(x => x[0]),
- parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName))));
- cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
- WPFUtils.CreateMultiBinding(
- CellFontStyleConverter.WrapConverter(x => x[0]),
- parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName))));
- cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
- WPFUtils.CreateMultiBinding(
- CellFontWeightConverter.WrapConverter(x => x[0]),
- parameter: new DynamicGridCellStyleParameters(column, DependencyProperty.UnsetValue))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName))));
- }
- #region Column Filtering
- private void ApplyFilterStyle(GridColumn column, bool filtering, bool allowSorting)
- {
- column.AllowFiltering = false;
- var filterstyle = new Style();
- if (filtering)
- {
- filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, DynamicGridUtils.FilterBackground));
- column.ImmediateUpdateColumnFilter = true;
- column.ColumnFilter = ColumnFilter.Value;
- column.FilterRowCondition = FilterRowCondition.Contains;
- column.FilterRowOptionsVisibility = Visibility.Collapsed;
- column.AllowBlankFilters = true;
- column.AllowSorting = allowSorting && Parent.CanSort();
- }
- else
- {
- filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- filterstyle.Setters.Add(new Setter(Control.IsEnabledProperty, false));
- column.ColumnFilter = ColumnFilter.Value;
- column.AllowFiltering = false;
- column.AllowSorting = false;
- column.FilterRowEditorType = "TextBox";
- column.FilterRowOptionsVisibility = Visibility.Collapsed;
- }
- column.FilterRowCellStyle = filterstyle;
- }
- private void Filter_FilterChanged(IDynamicGridColumnFilter filter)
- {
- AddRows(Parent.Data.Rows, true);
- Parent.UIFilterChanged(this);
- }
- private bool FilterRow(CoreRow row)
- {
- foreach(var column in ColumnList)
- {
- if(Parent.GetColumnFilter(column) is IDynamicGridColumnFilter filter && !filter.FilterRow(row))
- {
- return false;
- }
- }
- return true;
- }
- private void SetFilterUIButton(GridColumn gridColumn, DynamicColumnBase column)
- {
- if (Parent.GetColumnFilter(column) is not IDynamicGridColumnFilter filter) return;
- var vertical = column is DynamicActionColumn ac && ac.VerticalHeader && !ac.HeaderText.IsNullOrWhiteSpace();
- var horizontalAlignment = gridColumn.HorizontalHeaderContentAlignment;
- gridColumn.HorizontalHeaderContentAlignment = HorizontalAlignment.Stretch;
- gridColumn.HeaderTemplate = TemplateGenerator.CreateDataTemplate(() =>
- {
- var grid = new Grid();
- grid.AddColumn(GridUnitType.Star);
- var filterCol = grid.AddColumn(GridUnitType.Auto);
- var content = new ContentControl();
- content.HorizontalAlignment = horizontalAlignment;
- content.VerticalAlignment = VerticalAlignment.Center;
- content.SetBinding(ContentControl.ContentProperty, new Binding());
- grid.AddChild(content, 0, 0);
- var button = new DynamicGridColumnFilterUIButton(filter);
- grid.AddChild(button, 0, 1);
- if(vertical)
- {
- button.LayoutTransform = new RotateTransform(90);
- content.HorizontalAlignment = HorizontalAlignment.Stretch;
- }
- return grid;
- });
- }
- #endregion
- #region Summaries
- public class SummaryRowStyleSelector(DynamicGridGridUIComponent<T> parent, Func<Style> selector) : StyleSelector
- {
- public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
- public Func<Style> Selector { get; set; } = selector;
- public override Style? SelectStyle(object item, DependencyObject container)
- {
- return Selector();
- }
- }
- public class SummaryCellStyleSelector(DynamicGridGridUIComponent<T> parent, Func<DynamicColumnBase, Style> selector) : StyleSelector
- {
- public DynamicGridGridUIComponent<T> Parent { get; init; } = parent;
- public Func<DynamicColumnBase, Style> Selector { get; set; } = selector;
- public override Style? SelectStyle(object item, DependencyObject container)
- {
- var vcol = ((GridTableSummaryCell)container).ColumnBase.ColumnIndex;
- var col = Parent.GetColumn(vcol);
- return col is not null ? Selector(col) : null;
- }
- }
- private GridSummaryColumn? ConvertSummary(IDynamicGridSummary? summary)
- {
- if(summary is DynamicGridSumSummary sum)
- {
- var newSummary = new GridSummaryColumn
- {
- Format = $"{{Sum:{sum.Format}}}"
- };
- if(sum.AggregateType == typeof(double))
- {
- newSummary.SummaryType = Syncfusion.Data.SummaryType.DoubleAggregate;
- }
- else if(sum.AggregateType == typeof(int))
- {
- newSummary.SummaryType = Syncfusion.Data.SummaryType.Int32Aggregate;
- }
- else if(sum.AggregateType == typeof(TimeSpan))
- {
- newSummary.SummaryType = Syncfusion.Data.SummaryType.Custom;
- newSummary.CustomAggregate = new DynamicGridDurationAggregate();
- }
- return newSummary;
- }
- else if(summary is DynamicGridCountSummary count)
- {
- return new GridSummaryColumn
- {
- Format = "{Count:N0}",
- SummaryType = Syncfusion.Data.SummaryType.CountAggregate
- };
- }
- else if(summary is DynamicGridCustomSummary custom)
- {
- return new GridSummaryColumn
- {
- Format = $"{{Sum:{custom.Format}}}",
- SummaryType = SummaryType.Custom,
- CustomAggregate = new InternalAggregate(this, custom.Aggregate)
- };
- }
- else if(summary is DynamicGridTemplateSummary template)
- {
- return new GridSummaryColumn
- {
- TemplateSelector = new FuncTemplateSelector((item, o) => template.Template())
- };
- }
- else
- {
- return null;
- }
- }
- private class InternalAggregate(DynamicGridGridUIComponent<T> grid, DynamicGridCustomSummary.AggregateFunc aggregate) : ISummaryAggregate
- {
- private DynamicGridCustomSummary.AggregateFunc Aggregate = aggregate;
- private DynamicGridGridUIComponent<T> Grid = grid;
- public object? Sum { get; set; } = null;
- public Action<IEnumerable, string, PropertyDescriptor> CalculateAggregateFunc() => CalculateAggregate;
- private void CalculateAggregate(IEnumerable items, string property, PropertyDescriptor args)
- {
- if (items is IEnumerable<DataRowView> rows)
- {
- Sum = Aggregate(rows.Select(x =>
- {
- return Grid.GetRow(x.Row);
- }).NotNull());
- }
- else
- {
- Logger.Send(LogType.Error, "", $"Attempting to calculate aggregate on invalid data type '{items.GetType()}'.");
- }
- }
- }
- #endregion
- private void LoadActionColumn(DynamicActionColumn column)
- {
- var i = ActionColumns.Count;
- var sColName = string.Format("ActionColumn{0}", i);
- ActionColumns.Add(column);
- gridRowResizingOptions.ExcludeColumns.Add(sColName);
- var summary = ConvertSummary(column.Summary());
- if (summary != null)
- {
- summary.Name = sColName;
- summary.MappingName = sColName;
- Summaries.Add(summary);
- }
- if (column is DynamicImageColumn imgcol)
- {
- var newcol = new GridImageColumn();
- newcol.MappingName = sColName;
- newcol.Width = column.Width == 0 ? DataGrid.RowHeight : column.Width;
- newcol.Padding = new Thickness(4);
- newcol.ImageHeight = DataGrid.RowHeight - 8;
- newcol.ImageWidth = DataGrid.RowHeight - 8;
- newcol.ColumnSizer = GridLengthUnitType.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowEditing = false;
- ApplyFilterStyle(newcol, true, false);
- SetFilterUIButton(newcol, column);
- newcol.ShowToolTip = column.ToolTip != null;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- var style = new Style();
- style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- style.Setters.Add(new Setter(Control.IsEnabledProperty, false));
- newcol.FilterRowCellStyle = style;
- newcol.HeaderStyle = GetHeaderCellStyle(column);
- DataGrid.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- else if (column is DynamicTextColumn txtCol)
- {
- var newcol = new GridTextColumn();
- gridRowResizingOptions.ExcludeColumns.Add(sColName);
- newcol.TextWrapping = TextWrapping.NoWrap;
- newcol.TextAlignment = txtCol.Alignment.TextAlignment(typeof(string));
- newcol.AllowEditing = false;
- newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
- newcol.MappingName = sColName;
- newcol.Width = column.Width;
- newcol.ColumnSizer = GridLengthUnitType.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowFiltering = (column.Filters != null && column.Filters.Length != 0) || column.FilterRecord is not null;
- newcol.AllowSorting = false;
- newcol.FilterRowOptionsVisibility = Visibility.Collapsed;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
-
- newcol.ShowToolTip = column.ToolTip != null;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- ApplyFilterStyle(newcol, true, true);
- SetFilterUIButton(newcol, column);
- newcol.HeaderStyle = GetHeaderCellStyle(column);
- var cellstyle = GetCellStyle(column);
- AddCellStyleConverters(cellstyle, column, sColName);
- newcol.CellStyle = cellstyle;
- DataGrid.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- else if (column is DynamicTemplateColumn tmplCol)
- {
- var newcol = new GridTemplateColumn();
- newcol.CellTemplate = TemplateGenerator.CreateDataTemplate(() =>
- {
- var content = new ContentControl();
- content.SetBinding(ContentControl.ContentProperty,
- WPFUtils.CreateMultiBinding(new MultiFuncConverter(x =>
- {
- if(x[0] is DataRowView view && DataGridItems is DataTable table && GetRow(view.Row) is CoreRow row)
- {
- return tmplCol.Template(row);
- }
- return null;
- }))
- .AddBinding(new Binding("."))
- .AddBinding(new Binding(sColName)));
- return content;
- });
- newcol.AllowEditing = false;
- newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
- newcol.MappingName = sColName;
-
- newcol.Width = tmplCol.Width;
- newcol.ColumnSizer = GridLengthUnitType.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowFiltering = false;
- newcol.AllowSorting = false;
- newcol.FilterRowOptionsVisibility = Visibility.Collapsed;
-
- newcol.ShowToolTip = column.ToolTip != null;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- SetFilterUIButton(newcol, column);
- var style = new Style();
- style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- style.Setters.Add(new Setter(Control.IsEnabledProperty, false));
- newcol.FilterRowCellStyle = style;
- newcol.HeaderStyle = GetHeaderCellStyle(column);
- var cellstyle = GetCellStyle(column);
- AddCellStyleConverters(cellstyle, column, sColName);
- newcol.CellStyle = cellstyle;
- DataGrid.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- }
- private void LoadDataColumn(DynamicGridColumn column)
- {
- if (this.CreateEditorColumn(column, out var newcol, out var prop))
- {
- newcol.GetEntity = () => _editingObject?.Object;
- newcol.EntityChanged += DoEntityChanged;
- if (!newcol.VariableHeight)
- gridRowResizingOptions.ExcludeColumns.Add(newcol.MappingName);
- var newColumn = newcol.CreateGridColumn();
- var summary = ConvertSummary(newcol.Summary());
- if (summary != null)
- {
- summary.Name = newcol.MappingName;
- summary.MappingName = newcol.MappingName;
- Summaries.Add(summary);
- }
-
- ApplyFilterStyle(newColumn, newcol.Filtered, true);
-
- newColumn.HeaderStyle = GetHeaderCellStyle(column);
- SetFilterUIButton(newColumn, column);
- newColumn.ShowHeaderToolTip = !column.Editor.ToolTip.IsNullOrWhiteSpace();
- var cellstyle = GetCellStyle(column);
- if (Parent.IsDirectEditMode())
- {
- var editor = Parent.CustomiseEditor(column, column.Editor);
- if (editor is null || !newcol.Editable || !editor.Editable.IsDirectEditable())
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- new Binding()
- {
- Path = new PropertyPath("."), Converter = CellBackgroundConverter,
- ConverterParameter = new DynamicGridCellStyleParameters(column,new SolidColorBrush(Colors.WhiteSmoke))
- }));
- newColumn.AllowEditing = false;
- }
- else
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- new Binding()
- {
- Path = new PropertyPath("."), Converter = CellBackgroundConverter,
- ConverterParameter = new DynamicGridCellStyleParameters(column,new SolidColorBrush(Colors.LightYellow))
- }));
- newColumn.AllowEditing = true;
- }
- cellstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
- newColumn.CellStyle = cellstyle;
- }
- else
- {
- AddCellStyleConverters(cellstyle, column, newColumn.MappingName);
- newColumn.CellStyle = cellstyle;
- }
-
- DataGrid.Columns.Add(newColumn);
- ColumnList.Add(column);
- foreach (var extra in newcol.ExtraColumns)
- Parent.AddHiddenColumn(extra);
- }
- }
- private void LoadSummaries()
- {
- if (Summaries.Any())
- {
- DataGrid.CellRenderers.Remove("TableSummary");
- DataGrid.CellRenderers.Add("TableSummary", new DynamicGridAggregateRenderer());
- DataGrid.TableSummaryRows.Add(new GridTableSummaryRow
- {
- ShowSummaryInRow = false,
- Position = TableSummaryRowPosition.Bottom,
- SummaryColumns = Summaries,
- });
- }
- }
- public void RefreshColumns(IEnumerable<DynamicColumnBase> columns, DynamicGridColumnGroupings groupings)
- {
- if (DataGrid.View != null)
- DataGrid.View.Filter = null;
-
- DataGrid.ItemsSource = null;
- DataGrid.Columns.Suspend();
- ColumnList.Clear();
- ActionColumns.Clear();
- DataGrid.Columns.Clear();
- DataGrid.TableSummaryRows.Clear();
- gridRowResizingOptions.ExcludeColumns = new List<string>();
- Summaries.Clear();
- foreach(var column in columns)
- {
- if(Parent.GetColumnFilter(column) is IDynamicGridColumnFilter filter)
- {
- filter.FilterChanged += Filter_FilterChanged;
- }
- if(column is DynamicActionColumn ac)
- {
- LoadActionColumn(ac);
- }
- else if(column is DynamicGridColumn gc)
- {
- LoadDataColumn(gc);
- }
- }
- LoadSummaries();
- LoadStackedHeaders(groupings);
- DataGrid.Columns.Resume();
- DataGrid.RefreshColumns();
- foreach (var key in _filterpredicates.Keys.ToArray())
- if (DataGrid.Columns.Any(x => string.Equals(x.MappingName, key)))
- {
- var predicates = Serialization.Deserialize<List<FilterPredicate>>(_filterpredicates[key]);
- foreach (var predicate in predicates)
- {
- DataGrid.Columns[key].FilterPredicates.Add(predicate);
- DataGrid.Columns[key].FilteredFrom = FilteredFrom.FilterRow;
- }
- }
- else
- {
- _filterpredicates.Remove(key);
- }
- ResizeColumns(DataGrid, DataGrid.ActualWidth - 2, DataGrid.ActualHeight - 2);
- if(groupings.Count > 0)
- {
- // THis here is to fix a problem with Syncfusion when we have stackedHeaderRows; the above setting of the ItemsSource to null
- // was causing the resetting of it to fail when reloading, due to some internal OutOfRange Exception. THe use case was selecting columns on the
- // Stock Forecast grid.
- RefreshData(new CoreTable());
- }
- if (DataGrid.View != null)
- DataGrid.View.Filter = (o) =>
- {
- return true;
- };
- }
- #endregion
- #region Data
- private bool _invalidating = false;
- private Dictionary<DataRow, CoreRow> _rowMap = new();
- public void BeforeRefresh()
- {
- DataGrid.SelectionForegroundBrush = GetCellSelectionForegroundBrush();
- DataGrid.RowSelectionBrush = GetCellSelectionBackgroundBrush();
- }
- public void RefreshData(CoreTable data)
- {
- var result = new DataTable();
- _rowMap.Clear();
- var defaults = new List<object?>();
- foreach (var column in data.Columns)
- {
- var colname = column.ColumnName.Replace('.', '_');
- if (!result.Columns.Contains(colname))
- {
- result.Columns.Add(colname, column.DataType);
- if (!Parent.IsDirectEditMode())
- defaults.Add(column.DataType.GetDefault());
- }
- }
- for (var i = 0; i < ActionColumns.Count; i++)
- result.Columns.Add(string.Format("ActionColumn{0}", i),
- ActionColumns[i] is DynamicImageColumn
- ? typeof(ImageSource)
- : typeof(String)
- );
- foreach (var row in data.Rows.Where(FilterRow))
- {
- var newrow = result.NewRow();
- CoreRowToDataRow(newrow, row, defaults);
- result.Rows.Add(newrow);
- _rowMap[newrow] = row;
- }
- result.ColumnChanged += Result_ColumnChanged;
- //int rowIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.RowIndex;
- //int columnIndex = DataGrid.SelectionController.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
- //int scrollRowIndex = DataGrid.GetVisualContainer().ScrollRows.LastBodyVisibleLineIndex;
- //this.DataGrid.ScrollInView(new Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex(scrollRowIndex, columnIndex));
- DataGrid.ItemsSource = result;
- ResizeColumns(DataGrid, DataGrid.ActualWidth - 1, DataGrid.ActualHeight);
- UpdateRecordCount();
- }
- private void AddRows(IEnumerable<CoreRow> rows, bool clearRows)
- {
- var table = DataGridItems;
- if(table is null)
- {
- return;
- }
- _invalidating = true;
- if (clearRows)
- {
- table.Rows.Clear();
- _rowMap.Clear();
- }
- var defaults = new List<object?>();
- if (!Parent.IsDirectEditMode())
- foreach (var column in table.Columns.Cast<DataColumn>())
- {
- defaults.Add(column.DataType.GetDefault());
- }
- foreach(var row in rows.Where(FilterRow))
- {
- var newRow = table.NewRow();
- CoreRowToDataRow(newRow, row, defaults);
- table.Rows.Add(newRow);
- _rowMap[newRow] = row;
- }
- UpdateRecordCount();
- _invalidating = false;
- }
- public void AddPage(IEnumerable<CoreRow> page)
- {
- AddRows(page, false);
- }
- public void InvalidateRow(CoreRow row)
- {
- var table = DataGridItems;
- if(table is null)
- {
- return;
- }
- _invalidating = true;
- var rowdata = new List<object?>(row.Values);
- foreach (var ac in ActionColumns)
- rowdata.Add(ac.Data(row));
- //DataGridItems.Rows.RemoveAt(row.Index);
- //var datarow = DataGridItems.NewRow();
- var datarow = DataGridItems.Rows[row.Index];
- for (var i = 0; i < rowdata.Count; i++)
- datarow[i] = rowdata[i] ?? DBNull.Value;
- //DataGridItems.Rows.InsertAt(datarow, row.Index);
- _invalidating = false;
- }
- private void CoreRowToDataRow(DataRow newrow, CoreRow row, List<object?> defaults)
- {
- var rowdata = new List<object?>(row.Values);
- foreach (var ac in ActionColumns)
- rowdata.Add(ac.Data(row));
- try
- {
- var data = ProcessRow(rowdata, defaults).ToArray();
- newrow.ItemArray = data;
- }
- catch (Exception)
- {
- throw;
- }
- }
- private static IEnumerable<object?> ProcessRow(List<object?> values, List<object?> defaults)
- {
- if (defaults == null || defaults.Count == 0)
- return values;
- var result = new List<object?>();
- for (var i = 0; i < values.Count; i++)
- {
- var value = values[i];
- var defaultvalue = i < defaults.Count ? defaults[i] : null;
- result.Add(value == null || (value.Equals(defaultvalue) && !value.GetType().IsEnum) ? null : value);
- }
- return result;
- }
- #endregion
- #region Direct Edit
- private bool bChanged;
- private class DirectEditingObject
- {
- public T Object { get; set; }
- public CoreRow Row { get; set; }
- public DataRow? DataRow { get; set; }
- public DirectEditingObject(T obj, CoreRow row, DataRow? dataRow)
- {
- Object = obj;
- Row = row;
- DataRow = dataRow;
- }
- }
- private DirectEditingObject? _editingObject;
- private DirectEditingObject EnsureEditingObject(CoreRow row)
- {
- _editingObject ??= new(Parent.LoadItem(row), row, DataGridItems?.Rows[row.Index]);
- return _editingObject;
- }
- private DataRow? GetDataRow(CoreRow row)
- {
- return DataGridItems?.Rows[row.Index];
- }
- void IDynamicGridUIComponent<T>.UpdateCell(CoreRow row, string column, object? value)
- {
- var dataRow = GetDataRow(row);
- var datacolname = column.Replace(".", "_");
- if(dataRow is not null)
- {
- dataRow[datacolname] = value ?? DBNull.Value;
- }
- }
- void IDynamicGridUIComponent<T>.UpdateCell(CoreRow row, DynamicColumnBase column)
- {
- var dataRow = GetDataRow(row);
- if(dataRow is not null)
- {
- if(column is DynamicGridColumn gc)
- {
- var dataColName = gc.ColumnName.Replace(".", "_");
- dataRow[dataColName] = row[gc.ColumnName] ?? DBNull.Value;
- }
- else if(column is DynamicActionColumn ac)
- {
- var i = ActionColumns.IndexOf(ac);
- dataRow[$"ActionColumn{i}"] = ac.Data(row);
- }
- }
- }
- void UpdateRow(CoreRow row, DataRow dataRow)
- {
- foreach(var (key, value) in row)
- {
- var datacolname = key.Replace(".", "_");
- var dataValue = dataRow[datacolname];
- if (!Equals(dataValue, value) && !(value is null && dataValue == DBNull.Value))
- {
- dataRow[datacolname] = value ?? DBNull.Value;
- }
- }
- for (var i = 0; i < ActionColumns.Count; i++)
- dataRow[$"ActionColumn{i}"] = ActionColumns[i].Data(row);
- }
- void IDynamicGridUIComponent<T>.UpdateRow(CoreRow row)
- {
- var dataRow = GetDataRow(row);
- if(dataRow is not null)
- {
- UpdateRow(row, dataRow);
- }
- }
- private void DoEntityChanged(IDynamicColumnBase column, DynamicColumnEntityChangedEventArgs args)
- {
- if (_editingObject is null) return;
- Parent.EntityChanged(_editingObject.Object, _editingObject.Row, args.ColumnName, args.Changes);
- }
- private void UpdateData(string column, Dictionary<CoreColumn, object?> updates)
- {
- if (_editingObject is null)
- return;
- var coreRow = _editingObject.Row;
- Parent.UpdateData(_editingObject.Object, coreRow, column, updates);
- }
- private void UpdateData(int rowIndex, int columnIndex)
- {
- var table = DataGridItems;
- if (table is null)
- return;
-
- if (GetColumn(columnIndex) is DynamicGridColumn gridcol)
- {
- var datacol = Parent.Data.Columns.FirstOrDefault(x => x.ColumnName.Equals(gridcol.ColumnName));
- if (datacol != null)
- {
- var datacolindex = Parent.Data.Columns.IndexOf(datacol);
- var value = table.Rows[rowIndex][datacolindex];
- if (value is DBNull)
- value = CoreUtils.GetDefault(datacol.DataType);
- UpdateData(datacol.ColumnName, new Dictionary<CoreColumn, object?>() { { datacol, value } });
- }
- }
- }
- private void DataGrid_CurrentCellBeginEdit(object? sender, CurrentCellBeginEditEventArgs e)
- {
- var table = DataGridItems;
- var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
- if (table is null || row is null)
- return;
- EnsureEditingObject(row);
- var column = DataGrid.Columns[e.RowColumnIndex.ColumnIndex] as GridComboBoxColumn;
- if (column != null && column.ItemsSource == null)
- {
- var colname = column.MappingName;
- var colno = table.Columns.IndexOf(colname);
- var property = Parent.Data.Columns[colno].ColumnName;
- var prop = CoreUtils.GetProperty(typeof(T), property);
- var editor = prop.GetEditor();
- if (editor is ILookupEditor lookupEditor)
- {
- if (!Lookups.ContainsKey(property))
- Lookups[property] = lookupEditor.Values(typeof(T), property);
- var combo = column;
- combo.ItemsSource = Lookups[property].ToDictionary(Lookups[property].Columns[0].ColumnName, "Display");
- combo.SelectedValuePath = "Key";
- combo.DisplayMemberPath = "Value";
- }
- }
- bChanged = false;
- }
- private void Result_ColumnChanged(object sender, DataColumnChangeEventArgs e)
- {
- if (_invalidating) return;
- if (sender is not DataTable table) return;
- var row = GetRow(e.Row);
- if (row is null)
- return;
- var colIdx = table.Columns.IndexOf(e.Column);
- if (colIdx < 0 || colIdx >= Parent.Data.Columns.Count)
- return;
- var data = Parent.Data;
- var dataCol = Parent.Data.Columns[colIdx];
- var col = ColumnList.OfType<DynamicGridColumn>()
- .FirstOrDefault(x => x.ColumnName.Equals(dataCol.ColumnName));
-
- if (col is null)
- return;
- if (col is DynamicGridCheckBoxColumn<T>)
- {
- EnsureEditingObject(row);
- if(_editingObject is not null)
- {
- var value = e.Row[e.Column!];
- if (value is DBNull)
- value = CoreUtils.GetDefault(dataCol.DataType);
- _invalidating = true;
- UpdateData(dataCol.ColumnName, new Dictionary<CoreColumn, object?>() { { dataCol, value } });
- _invalidating = false;
- }
- _editingObject = null;
- }
- if (_editingObject is not null)
- bChanged = true;
- }
- private void CancelEdit()
- {
- var obj = _editingObject;
- bChanged = false;
- _editingObject = null;
- DataGrid.SelectionController.CurrentCellManager.EndEdit(false);
- if(obj is not null)
- {
- UpdateRow(obj.Row, obj.DataRow);
- }
- }
- private void DataGrid_CurrentCellDropDownSelectionChanged(object? sender,
- CurrentCellDropDownSelectionChangedEventArgs e)
- {
- var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
- if (row is null)
- return;
- EnsureEditingObject(row);
- if ((_editingObject is not null) && (e.SelectedItem is Tuple<object?, string> tuple))
- {
- var mappedname = DataGrid.Columns[e.RowColumnIndex.ColumnIndex].MappingName;
- var colno = DataGridItems.Columns.IndexOf(mappedname);
- var corecol = Parent.Data.Columns[colno].ColumnName;
- var updates = new Dictionary<CoreColumn, object?>();
- var prefix = String.Join(".", corecol.Split(".").Reverse().Skip(1).Reverse());
- var field = corecol.Split(".").Last();
- var prop = CoreUtils.GetProperty(typeof(T), corecol);
- if (prop.GetEditor() is ILookupEditor editor)
- {
- var data = editor.Values(typeof(T), corecol);
- var lookuprow = data.Rows.FirstOrDefault(r => Equals(r[field], tuple.Item1))
- ?? data.NewRow(true);
- foreach (CoreColumn lookupcol in data.Columns)
- {
- var columnname = String.IsNullOrWhiteSpace(prefix)
- ? lookupcol.ColumnName
- : String.Join(".", prefix, lookupcol.ColumnName);
- var updatecol = Parent.Data.Columns.FirstOrDefault(x => String.Equals(x.ColumnName, columnname));
- if (updatecol != null)
- updates[updatecol] = lookuprow[lookupcol.ColumnName];
- }
- UpdateData(corecol, updates);
- bChanged = true;
-
- }
- }
- }
- private void DataGrid_CurrentCellEndEdit(object? sender, CurrentCellEndEditEventArgs e)
- {
- if (_editingObject is not null && bChanged)
- {
- UpdateData(_editingObject.Row.Index, e.RowColumnIndex.ColumnIndex);
- }
- if (bChanged)
- Parent.DoChanged();
- bChanged = false;
- _editingObject = null;
- // 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
- // editing the filter row. It seems that this causes Syncfusion to commit the filter predicates internally, which means that after leaving a
- // filter row cell, the filter remained even once it was cleared, meaning a refresh was necessary to get the data back.
- // I've tested on Bills to see if editing works with this empty, and it seems so.
- //DataGridItems?.AcceptChanges();
- }
- private void DataGrid_PreviewKeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.OemPeriod)
- {
- if (e.OriginalSource is TimeSpanEdit editor && editor.SelectionStart < 2)
- {
- editor.SelectionStart = 3;
- }
- }
- else if (e.Key == Key.Tab)
- {
- if (Parent.IsDirectEditMode())
- {
- DataGrid.SelectionController.CurrentCellManager.EndEdit();
- DataGrid.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
- DataGrid.SelectionController.CurrentCellManager.BeginEdit();
- e.Handled = true;
- }
- }
- else if(e.Key == Key.Escape)
- {
- if (Parent.IsDirectEditMode())
- {
- bChanged = false;
- }
- }
- }
- #endregion
- #region Drag + Drop
- private void DataGrid_DragOver(object sender, DragEventArgs e)
- {
- if (!Parent.Options.DragTarget)
- {
- return;
- }
- Parent.DragOver(sender, e);
- }
- private void DataGrid_Drop(object sender, DragEventArgs e)
- {
- if (!Parent.Options.DragTarget)
- {
- return;
- }
- Parent.Drop(sender, e);
- }
- private void RowDragDropController_DragStart(object? sender, GridRowDragStartEventArgs e)
- {
- var rows = e.DraggingRecords.Select(record =>
- {
- var rowIndex = DataGrid.ResolveToRowIndex(record);
- return GetRowFromIndex(rowIndex);
- }).NotNull().ToArray();
- Parent.DragStart(sender, rows);
- }
- private void RowDragDropController_Drop(object? sender, GridRowDropEventArgs e)
- {
- if (!Parent.Options.ReorderRows)
- {
- e.Handled = true;
- }
- e.Handled = true;
- }
- private void RowDragDropController_Dropped(object? sender, GridRowDroppedEventArgs e)
- {
- if (!Parent.Options.ReorderRows)
- {
- }
- if(e.DropPosition != DropPosition.None)
- {
- var records = (e.Data.GetData("Records") as ObservableCollection<object>)!;
- var targetIdx = (int)e.TargetRecord;
- var rows = records.Select(x =>
- {
- if (x is DataRowView dataRow)
- {
- return GetRow(dataRow.Row);
- }
- else
- {
- return null;
- }
- }).NotNull().OrderBy(x => x.Index).ToArray();
- Parent.MoveRows(rows, e.DropPosition == DropPosition.DropBelow ? targetIdx + 1 : targetIdx);
- }
- }
- private void RowDragDropController_DragOver(object? sender, GridRowDragOverEventArgs e)
- {
- if (!Parent.Options.ReorderRows)
- {
- e.Handled = true;
- }
- }
- #endregion
- }
|