123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962 |
- using InABox.Clients;
- using InABox.Core;
- using InABox.Wpf;
- using InABox.WPF;
- using Syncfusion.Data;
- using Syncfusion.UI.Xaml.Grid;
- using Syncfusion.UI.Xaml.ScrollAxis;
- using Syncfusion.UI.Xaml.TreeGrid;
- using Syncfusion.UI.Xaml.TreeGrid.Helpers;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace InABox.DynamicGrid;
- public class DynamicGridTreeUIComponent<T> : IDynamicGridUIComponent<T>, IDynamicGridGridUIComponent<T>
- where T : BaseObject, new()
- {
- private IDynamicGridUIComponentParent<T> _parent;
- public IDynamicGridUIComponentParent<T> Parent
- {
- get => _parent;
- set
- {
- _parent = value;
- CellBackgroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellBackground);
- CellForegroundConverter = new DynamicGridCellStyleConverter<System.Windows.Media.Brush?>(Parent, GetCellForeground);
- CellFontSizeConverter = new DynamicGridCellStyleConverter<double?>(Parent, GetCellFontSize);
- CellFontStyleConverter = new DynamicGridCellStyleConverter<System.Windows.FontStyle?>(Parent, GetCellFontStyle);
- CellFontWeightConverter = new DynamicGridCellStyleConverter<System.Windows.FontWeight?>(Parent, GetCellFontWeight);
- Parent.AddHiddenColumn(IDColumn.Property);
- Parent.AddHiddenColumn(ParentColumn.Property);
- Parent.AddHiddenColumn(DescriptionColumn.Property);
- }
- }
- private Column<T> IDColumn;
- private Column<T> ParentColumn;
- private Column<T> DescriptionColumn;
- private ContextMenu _menu;
- private SfTreeGrid _tree;
- private readonly ContextMenu ColumnsMenu;
- public event OnContextMenuOpening OnContextMenuOpening;
- FrameworkElement IDynamicGridUIComponent<T>.Control => _tree;
- private bool _shownumbers = false;
- public bool ShowNumbers
- {
- get => _shownumbers;
- set
- {
- _shownumbers = value;
- _tree.Columns[1].Width = value ? 50 : 0;
- }
- }
- private double minRowHeight = 30D;
- private double maxRowHeight = 30D;
- public double MinRowHeight
- {
- get => minRowHeight;
- set
- {
- minRowHeight = value;
- CalculateRowHeight();
- }
- }
- public double MaxRowHeight
- {
- get => maxRowHeight;
- set
- {
- maxRowHeight = value;
- CalculateRowHeight();
- }
- }
- #region IDynamicGridGridUIComponent
- IList<DynamicColumnBase> IDynamicGridGridUIComponent<T>.ColumnList => ColumnList;
- int IDynamicGridGridUIComponent<T>.RowHeight => (int)RowHeight;
- #endregion
- private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellBackgroundConverter;
- private DynamicGridCellStyleConverter<System.Windows.Media.Brush?> CellForegroundConverter;
- private DynamicGridCellStyleConverter<double?> CellFontSizeConverter;
- private DynamicGridCellStyleConverter<System.Windows.FontStyle?> CellFontStyleConverter;
- private DynamicGridCellStyleConverter<System.Windows.FontWeight?> CellFontWeightConverter;
- protected virtual Brush? GetCellBackground(CoreRow row, String columnname) => null;
- protected virtual Brush? GetCellForeground(CoreRow row, String columnname) => null;
- protected virtual double? GetCellFontSize(CoreRow row, String columnname) => null;
- protected virtual FontStyle? GetCellFontStyle(CoreRow row, String columnname) => null;
- protected virtual FontWeight? GetCellFontWeight(CoreRow row, String columnname) => null;
- public DynamicGridTreeUIComponent(Expression<Func<T, Guid>> idColumn, Expression<Func<T, Guid>> parentIDColumn, Expression<Func<T, string>> descriptionColumn)
- {
- IDColumn = new Column<T>(CoreUtils.GetFullPropertyName(idColumn, "."));
- ParentColumn = new Column<T>(CoreUtils.GetFullPropertyName(parentIDColumn, "."));
- DescriptionColumn = new Column<T>(CoreUtils.GetFullPropertyName(descriptionColumn, "."));
- ColumnsMenu = new ContextMenu();
- ColumnsMenu.Opened += ColumnsMenu_ContextMenuOpening;
- _tree = new SfTreeGrid();
- _tree.ChildPropertyName = "Children";
- //_tree.ParentPropertyName = "Parent";
- _tree.AutoGenerateColumns = false;
- _tree.AutoExpandMode = AutoExpandMode.AllNodesExpanded;
- //_tree.NodeCollapsing += (o, e) => { e.Cancel = true; };
- //_tree.HeaderRowHeight = 0D;
- _tree.HeaderRowHeight = 30;
- _tree.HeaderContextMenu = ColumnsMenu;
- _tree.SelectionChanging += _tree_SelectionChanging;
- _tree.SelectionChanged += _tree_SelectionChanged;
- _tree.AllowSelectionOnExpanderClick = false;
- _tree.AllowAutoSizingExpanderColumn = false;
- _tree.CellTapped += _tree_CellTapped;
- _tree.CellDoubleTapped += _tree_CellDoubleTapped;
- _tree.KeyUp += _tree_KeyUp;
- _tree.CellToolTipOpening += _tree_CellToolTipOpening;
- _menu = new ContextMenu();
- var additem = new MenuItem() { Header = "Add Child Folder" };
- additem.Click += (o, e) => { DoAddItem((_tree.SelectedItem as CoreTreeNode)!.ID, true); };
- _menu.Items.Add(additem);
- _tree.ContextMenuOpening += _tree_ContextMenuOpening;
- _tree.ContextMenu = _menu;
- _tree.Background = new SolidColorBrush(Colors.DimGray);
-
- var cellStyle = new Style(typeof(TreeGridRowControl));
- cellStyle.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.White)));
- _tree.RowStyle = cellStyle;
- _tree.FilterChanged += _tree_FilterChanged;
- _tree.FilterItemsPopulating += _tree_FilterItemsPopulating;
-
- _tree.SelectionForeground = DynamicGridUtils.SelectionForeground;
- _tree.SelectionBackground = DynamicGridUtils.SelectionBackground;
-
- _tree.ColumnSizer = TreeColumnSizer.None;
- _tree.RowHeight = 30D;
- _tree.SetValue(Grid.RowProperty, 0);
- _tree.AllowDraggingRows = false;
- _tree.Drop += _tree_Drop;
- _tree.DragOver += _tree_DragOver;
- _tree.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;
- });
- _tree.SizeChanged += _tree_SizeChanged;
- }
- #region Public Interface
- public IEnumerable<CoreRow> GetChildren(Guid id)
- {
- return Nodes.GetChildren(id).Select(x => MapRow(x.Row));
- }
- #endregion
- #region Input
- 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 = _tree.GetNodeAtRowIndex(rowIndex);
- return MapRow((row.Item as CoreTreeNode)?.Row);
- }
- private void _tree_CellDoubleTapped(object? sender, TreeGridCellDoubleTappedEventArgs e)
- {
- _tree.Dispatcher.BeginInvoke(() =>
- {
- // This needs to happen outside the event handler, because the items source for the tree view might change during this method, and that causes an internal exception in Syncfusion. We need to finish the event before resetting the items source.
- Parent.DoubleClickCell(GetRowFromIndex(e.RowColumnIndex.RowIndex), GetColumn(e.RowColumnIndex.ColumnIndex));
- });
- }
- private void _tree_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
- {
- if (sender != _tree) return;
- Parent.HandleKey(e);
- }
- private void _tree_CellTapped(object? sender, TreeGridCellTappedEventArgs e)
- {
- if (!_tree.IsEnabled)
- return;
- if (GetColumn(e.RowColumnIndex.ColumnIndex) is not DynamicColumnBase column)
- return;
- if(e.ChangedButton == MouseButton.Left)
- {
- if(column is DynamicActionColumn dac)
- {
- Parent.ExecuteActionColumn(dac, SelectedRows);
- }
- }
- else if(e.ChangedButton == MouseButton.Right)
- {
- if(column is DynamicMenuColumn dmc)
- {
- Parent.ExecuteActionColumn(dmc, null);
- }
- else
- {
- Parent.OpenColumnMenu(column);
- }
- }
- }
- #endregion
- #region Selection
- public CoreRow[] SelectedRows
- {
- get
- {
- return _tree.SelectedItems.OfType<CoreTreeNode>()
- .Select(x => GetRow(x)).NotNull().ToArray();
- }
- set
- {
- _tree.SelectedItems.Clear();
- foreach (var row in value)
- {
- _tree.SelectedItems.Add(Nodes.Find(row.Get<Guid>(IDColumn.Property)));
- }
- }
- }
- private void _tree_SelectionChanged(object? sender, GridSelectionChangedEventArgs e)
- {
- var row = GetRow(_tree.SelectedItem as CoreTreeNode);
- if (row is not null)
- {
- Parent.SelectItems(new[] { row });
- }
- else
- {
- Parent.SelectItems(Array.Empty<CoreRow>());
- }
- }
- private void _tree_SelectionChanging(object? sender, GridSelectionChangingEventArgs e)
- {
- var cancel = new CancelEventArgs();
- Parent.BeforeSelection(cancel);
- if (cancel.Cancel)
- {
- e.Cancel = true;
- }
- }
- #endregion
- private void _tree_FilterItemsPopulating(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterItemsPopulatingEventArgs e)
- {
- var col = _tree.Columns.IndexOf(e.Column);
- if (GetColumn(col) is DynamicActionColumn column && column.Filters is not null)
- e.ItemsSource = column.Filters.Select(x => new FilterElement
- { DisplayText = x, ActualValue = x, IsSelected = column.SelectedFilters is null || column.SelectedFilters.Contains(x) });
- }
- private void _tree_FilterChanged(object? sender, Syncfusion.UI.Xaml.TreeGrid.Filtering.TreeGridFilterChangedEventArgs e)
- {
- var col = _tree.Columns.IndexOf(e.Column);
- if (GetColumn(col) is DynamicActionColumn column)
- {
- if (e.FilterPredicates != null)
- {
- var filter = e.FilterPredicates.Select(x => x.FilterValue.ToString()!).ToArray();
- bool include = e.FilterPredicates.Any(x => x.FilterType == FilterType.Equals);
- column.SelectedFilters = include ? filter : (column.Filters ?? Enumerable.Empty<string>()).Except(filter).ToArray();
- }
- else
- column.SelectedFilters = Array.Empty<string>();
- _tree.ClearFilter(e.Column);
- //e.FilterPredicates?.Clear();
- //e.FilterPredicates?.Add(new FilterPredicate() { PredicateType = PredicateType.Or, FilterBehavior = Syncfusion.Data.FilterBehavior.StringTyped, FilterMode = ColumnFilter.DisplayText, FilterType = Syncfusion.Data.FilterType.NotEquals, FilterValue = "" });
- //e.FilterPredicates?.Add(new FilterPredicate() { PredicateType = PredicateType.Or, FilterBehavior = Syncfusion.Data.FilterBehavior.StringTyped, FilterMode = ColumnFilter.DisplayText, FilterType = Syncfusion.Data.FilterType.Equals, FilterValue = "" });
- Parent.Refresh(false, false);
- }
- 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);
- }
- UpdateRecordCount();
- }
- private CoreRow? GetRow(CoreTreeNode? node)
- {
- return MapRow(node?.Row);
- }
- private CoreRow? MapRow(CoreRow? row)
- {
- if (row is null) return null;
- var index = row.Index;
- if (index < 0 || index >= Parent.Data.Rows.Count) return null;
- return Parent.Data.Rows[row.Index];
- }
- private void ColumnsMenu_ContextMenuOpening(object sender, RoutedEventArgs e)
- {
- if (sender is not ContextMenu menu) return;
- menu.Items.Clear();
- Parent.LoadColumnsMenu(menu);
- }
- public bool OptionsChanged()
- {
- ColumnsMenu.Visibility = Parent.HasOption(DynamicGridOption.SelectColumns) ? Visibility.Visible : Visibility.Hidden;
- _tree.AllowFiltering = Parent.HasOption(DynamicGridOption.FilterRows);
- if (Parent.HasOption(DynamicGridOption.DragSource))
- {
- if (!_tree.AllowDraggingRows)
- {
- _tree.AllowDraggingRows = true;
- _tree.RowDragDropController.DragStart += RowDragDropController_DragStart;
- }
- }
- else
- {
- if (_tree.AllowDraggingRows)
- {
- _tree.AllowDraggingRows = false;
- _tree.RowDragDropController.DragStart -= RowDragDropController_DragStart;
- }
- }
-
- _tree.AllowDrop = Parent.HasOption(DynamicGridOption.DragTarget);
- _tree.SelectionMode = Parent.HasOption(DynamicGridOption.MultiSelect) ? GridSelectionMode.Extended : GridSelectionMode.Single;
- return false;
- }
- private void _tree_CellToolTipOpening(object? sender, TreeGridCellToolTipOpeningEventArgs e)
- {
- if (GetColumn(e.RowColumnIndex.ColumnIndex) is not DynamicActionColumn col)
- return;
- var toolTip = col.ToolTip;
- if (toolTip is null)
- return;
- var row = GetRowFromIndex(e.RowColumnIndex.RowIndex);
- e.ToolTip.Template = TemplateGenerator.CreateControlTemplate(
- typeof(ToolTip),
- () => toolTip.Invoke(col, row)
- );
- }
- #region Sizing
- public double RowHeight
- {
- get => _tree.RowHeight;
- set => _tree.RowHeight = value;
- }
- public double HeaderRowHeight
- {
- get => _tree.HeaderRowHeight;
- set => _tree.HeaderRowHeight = value;
- }
- private void _tree_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- CalculateRowHeight();
- if (Parent.IsReady && !Parent.IsRefreshing)
- ResizeColumns(_tree, e.NewSize.Width - 2, e.NewSize.Height - 2);
- }
- int IDynamicGridUIComponent<T>.DesiredWidth()
- {
- return this.DesiredWidth();
- }
- #endregion
- #region Context Menu
- private void _tree_ContextMenuOpening(object sender, ContextMenuEventArgs e)
- {
- _menu.Items.Clear();
- if (OnContextMenuOpening is not null)
- {
- OnContextMenuOpening.Invoke((_tree.SelectedItem as CoreTreeNode)!, _menu);
- if(_menu.Items.Count == 0)
- {
- e.Handled = true;
- }
- }
- else
- {
- if (Parent.HasOption(DynamicGridOption.AddRows))
- {
- _menu.AddItem("Add Item", null, (_tree.SelectedItem as CoreTreeNode)!.ID, (id) => DoAddItem(id, true));
- }
- }
- }
- #endregion
- #region CRUD
- protected T DoCreateItem(Guid parent)
- {
- var result = Parent.CreateItem();
- CoreUtils.SetPropertyValue(result, ParentColumn.Property, parent);
- return result;
- }
-
- protected void DoAddItem(Guid id, bool edit)
- {
- try
- {
- var item = DoCreateItem(id);
- if (edit)
- {
- if (Parent.EditItems(new[] { item }))
- {
- Parent.DoChanged();
- Parent.Refresh(false, true);
- }
- }
- else
- {
- Parent.SaveItem(item);
- Parent.DoChanged();
- Parent.Refresh(false, true);
- }
- }
- catch (Exception e)
- {
- MessageWindow.ShowError("An error occurred while adding an item", e);
- }
- }
- #endregion
- #region Columns
- private readonly List<DynamicColumnBase> ColumnList = new();
- private List<DynamicActionColumn> ActionColumns = new();
- private readonly Dictionary<string, string> FilterPredicates = new();
- private DynamicColumnBase? GetColumn(int index) =>
- index >= 0 && index < ColumnList.Count ? ColumnList[index] : null;
- private void ApplyFilterStyle(TreeGridColumn column, bool filtering, bool isactioncolumn)
- {
- var filterstyle = new Style();
- if (filtering)
- {
- filterstyle.Setters.Add(new Setter(Control.BackgroundProperty, DynamicGridUtils.FilterBackground));
- column.ImmediateUpdateColumnFilter = true;
- column.ColumnFilter = ColumnFilter.Value;
- column.AllowBlankFilters = true;
- column.AllowSorting = isactioncolumn
- ? false
- : 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;
- }
- }
- public class TemplateColumnSelector : DataTemplateSelector
- {
- public Func<FrameworkElement> DataTemplate { get; init; }
-
- public override DataTemplate SelectTemplate(object item, DependencyObject container)
- => TemplateGenerator.CreateDataTemplate(DataTemplate);
- }
- private void LoadActionColumns(DynamicActionColumnPosition position)
- {
- for (var i = 0; i < ActionColumns.Count; i++)
- {
- var column = ActionColumns[i];
- if (column.Position == position)
- {
- var sColName = $"[_ActionColumn{i}]";
- if (column is DynamicImageColumn imgcol)
- {
- var newcol = new TreeGridTemplateColumn();
- newcol.CellTemplateSelector = new TemplateColumnSelector() { DataTemplate = () =>
- {
- var image = new Image
- {
- Width = _tree.RowHeight - 8,
- Height = _tree.RowHeight - 8,
- };
- image.SetBinding(Image.SourceProperty, new Binding(sColName));
- return image;
- } };
- newcol.AllowEditing = false;
- newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
- newcol.Width = column.Width == 0 ? _tree.RowHeight : column.Width;
- newcol.Padding = new Thickness(4);
- newcol.ColumnSizer = TreeColumnSizer.None;
- newcol.HeaderText = column.HeaderText;
- ApplyFilterStyle(newcol, true, true);
- newcol.ShowToolTip = column.ToolTip != null;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- var headstyle = new Style(typeof(TreeGridHeaderCell));
- 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 (!string.IsNullOrWhiteSpace(column.HeaderText))
- {
- //headstyle.Setters.Add(new Setter(LayoutTransformProperty, new RotateTransform(270.0F)));
- 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, 0.75, 0.75)));
- if (imgcol.VerticalHeader)
- headstyle.Setters.Add(new Setter(Control.TemplateProperty,
- Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
- }
- else
- {
- var image = imgcol.Image?.Invoke(null);
- if (image != null)
- {
- var template = new DataTemplate(typeof(TreeGridHeaderCell));
- var border = new FrameworkElementFactory(typeof(Border));
- border.SetValue(Border.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro));
- border.SetValue(Border.PaddingProperty, new Thickness(4));
- border.SetValue(Control.MarginProperty, new Thickness(0, 0, 1, 1));
- var img = new FrameworkElementFactory(typeof(Image));
- img.SetValue(Image.SourceProperty, image);
- border.AppendChild(img);
- template.VisualTree = border;
- headstyle.Setters.Add(new Setter(TreeGridHeaderCell.PaddingProperty, new Thickness(0)));
- headstyle.Setters.Add(new Setter(TreeGridHeaderCell.ContentTemplateProperty, template));
- }
- }
- newcol.HeaderStyle = headstyle;
- _tree.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- else if (column is DynamicTextColumn txtCol)
- {
- var newcol = new TreeGridTextColumn();
- newcol.TextWrapping = TextWrapping.NoWrap;
- newcol.TextAlignment = txtCol.Alignment == Alignment.NotSet
- ? TextAlignment.Left
- : txtCol.Alignment == Alignment.BottomLeft || txtCol.Alignment == Alignment.MiddleLeft ||
- txtCol.Alignment == Alignment.TopLeft
- ? TextAlignment.Left
- : txtCol.Alignment == Alignment.BottomCenter || txtCol.Alignment == Alignment.MiddleCenter ||
- txtCol.Alignment == Alignment.TopCenter
- ? TextAlignment.Center
- : TextAlignment.Right;
- newcol.AllowEditing = false;
- newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
- newcol.MappingName = sColName;
- newcol.Width = column.Width;
- newcol.ColumnSizer = TreeColumnSizer.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowFiltering = column.Filters != null && column.Filters.Any();
- newcol.AllowSorting = false;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- var headstyle = new Style(typeof(TreeGridHeaderCell));
- 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.MarginProperty, new Thickness(0, -0.75, 0, 0.75)));
- headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.75)));
- if (txtCol.VerticalHeader)
- {
- headstyle.Setters.Add(new Setter(Control.HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
- headstyle.Setters.Add(new Setter(Control.TemplateProperty,
- Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
- }
- newcol.HeaderStyle = headstyle;
- _tree.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- else if (column is DynamicTemplateColumn tmplCol)
- {
- var newcol = new TreeGridTemplateColumn();
- newcol.CellTemplateSelector = new TemplateColumnSelector() { DataTemplate = tmplCol.Template };
- newcol.AllowEditing = false;
- newcol.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
-
- newcol.Width = tmplCol.Width;
- newcol.ColumnSizer = TreeColumnSizer.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowFiltering = false;
- newcol.AllowSorting = false;
- newcol.ShowToolTip = false;
- newcol.ShowHeaderToolTip = false;
- var headstyle = new Style(typeof(TreeGridHeaderCell));
- 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.MarginProperty, new Thickness(0, -0.75, 0, 0.75)));
- headstyle.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(0.75)));
- newcol.HeaderStyle = headstyle;
- _tree.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- }
- }
- }
- private void LoadDataColumns(DynamicGridColumns columns)
- {
- foreach (var column in columns)
- {
- if(this.CreateEditorColumn(column, out var newcol, out var prop))
- {
- //newcol.GetEntity = () => _editingObject.Object;
- //newcol.EntityChanged += DoEntityChanged;
- var newColumn = newcol.CreateTreeGridColumn();
- //newColumn.AllowEditing = newcol.Editable && Parent.IsDirectEditMode();
- ApplyFilterStyle(newColumn, newcol.Filtered, false);
-
- var headstyle = new Style(typeof(TreeGridHeaderCell));
- 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));
- newColumn.HeaderStyle = headstyle;
- var cellstyle = new Style();
- if (Parent.IsDirectEditMode())
- {
- if (prop.Editor is null || !prop.Editor.Editable.IsDirectEditable())
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- new SolidColorBrush(Colors.WhiteSmoke)));
- newColumn.AllowEditing = false;
- }
- else
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- new SolidColorBrush(Colors.LightYellow)));
- newColumn.AllowEditing = true;
- }
- cellstyle.Setters.Add(new Setter(Control.ForegroundProperty, new SolidColorBrush(Colors.Black)));
- newColumn.CellStyle = cellstyle;
- }
- else
- {
- cellstyle.Setters.Add(new Setter(Control.BackgroundProperty,
- new Binding()
- {
- Path = new PropertyPath("."), Converter = CellBackgroundConverter,
- ConverterParameter = column.ColumnName
- }));
- cellstyle.Setters.Add(new Setter(Control.ForegroundProperty,
- new Binding()
- { Converter = CellForegroundConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(Control.FontSizeProperty,
- new Binding()
- { Converter = CellFontSizeConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(Control.FontStyleProperty,
- new Binding()
- { Converter = CellFontStyleConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(Control.FontWeightProperty,
- new Binding()
- { Converter = CellFontWeightConverter, ConverterParameter = column.ColumnName }));
- newColumn.CellStyle = cellstyle;
- }
-
- _tree.Columns.Add(newColumn);
- ColumnList.Add(column);
- foreach (var extra in newcol.ExtraColumns)
- Parent.AddHiddenColumn(extra);
- }
- }
- }
- public void RefreshColumns(DynamicGridColumns columns, DynamicActionColumns actionColumns)
- {
- _tree.ItemsSource = null;
- _tree.Columns.Suspend();
- ColumnList.Clear();
- _tree.Columns.Clear();
- ActionColumns = actionColumns.ToList();
-
- //_tree.Columns.Add(new TreeGridTextColumn()
- // {
- // MappingName = "Number",
- // Width = _shownumbers ? 50 : 0,
- // TextAlignment = TextAlignment.Right
- // }
- //);
- LoadActionColumns(DynamicActionColumnPosition.Start);
- LoadDataColumns(columns);
- LoadActionColumns(DynamicActionColumnPosition.End);
- _tree.Columns.Resume();
- _tree.RefreshColumns();
- foreach (var key in FilterPredicates.Keys.ToArray())
- if (_tree.Columns.Any(x => string.Equals(x.MappingName, key)))
- {
- var predicates = Serialization.Deserialize<List<FilterPredicate>>(FilterPredicates[key]);
- foreach (var predicate in predicates)
- {
- _tree.Columns[key].FilterPredicates.Add(predicate);
- }
- }
- else
- {
- FilterPredicates.Remove(key);
- }
- ResizeColumns(_tree, _tree.ActualWidth - 2, _tree.ActualHeight - 2);
- }
- private void ResizeColumns(SfTreeGrid grid, double width, double height)
- {
- if (Parent.Data == null || width <= 0)
- return;
- grid.Dispatcher.BeginInvoke(() =>
- {
- foreach (var (index, size) in this.CalculateColumnSizes(width))
- _tree.Columns[index].Width = Math.Max(0.0F, size);
- });
- }
- #endregion
- #region Refresh
- public CoreTreeNodes Nodes { get; set; }
- private CoreTable? _innerTable;
- public void BeforeRefresh()
- {
- _tree.SelectionForeground = DynamicGridUtils.SelectionForeground;
- _tree.SelectionBackground = DynamicGridUtils.SelectionBackground;
- }
- public void RefreshData(CoreTable data)
- {
- var nodes = new CoreTreeNodes();
- _innerTable = new CoreTable();
- _innerTable.LoadColumns(data.Columns);
- for (var i = 0; i < ActionColumns.Count; i++)
- _innerTable.Columns.Add(
- new CoreColumn
- {
- ColumnName = $"_ActionColumn{i}",
- DataType = ActionColumns[i] is DynamicImageColumn
- ? typeof(BitmapImage)
- : typeof(String)
- });
- foreach (var row in data.Rows)
- {
- var newRow = _innerTable.NewRow();
- newRow.LoadValues(row.Values);
- for (var i = 0; i < ActionColumns.Count; i++)
- {
- var ac = ActionColumns[i];
- newRow[$"_ActionColumn{i}"] = ac.Data(row);
- }
- _innerTable.Rows.Add(newRow);
- var _id = row.Get<Guid>(IDColumn.Property);
- var _parent = row.Get<Guid>(ParentColumn.Property);
- var _description = row.Get<string>(DescriptionColumn.Property);
- nodes.Add(_id, _parent, newRow).Description = _description;
- }
- Nodes = nodes;
- _tree.ItemsSource = nodes.Nodes;
- CalculateRowHeight();
- ResizeColumns(_tree, _tree.ActualWidth - 1, _tree.ActualHeight);
- UpdateRecordCount();
- }
- private void CalculateRowHeight()
- {
- if(Parent.Data != null && Parent.Data.Rows.Count > 0)
- {
- var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
- var targetHeight = contentHeight / Parent.Data.Rows.Count;
- _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
- }
- }
- private void UpdateRecordCount()
- {
- var count = _tree.View != null ? _tree.View.Nodes.Count : Parent.Data.Rows.Count;
- Parent.UpdateRecordCount(count);
- }
- #endregion
- public void AddVisualFilter(string column, string value, FilterType filtertype = FilterType.Contains)
- {
- if (value.IsNullOrWhiteSpace())
- return;
- var col = _tree.Columns.FirstOrDefault((x => string.Equals(x.MappingName?.ToUpper(),column?.Replace(".", "_").ToUpper())));
- if (col != null)
- {
- col.FilterPredicates.Add(new FilterPredicate { FilterType = filtertype, FilterValue = value });
- }
- }
- public List<Tuple<string, FilterPredicate>> GetFilterPredicates()
- {
- var list = new List<Tuple<string, FilterPredicate>>();
- foreach (var column in _tree.Columns)
- {
- var colIndex = _tree.Columns.IndexOf(column);
- var col = ColumnList[colIndex];
- if (col is DynamicGridColumn gridColumn)
- {
- foreach (var predicate in column.FilterPredicates)
- {
- list.Add(new(gridColumn.ColumnName, predicate));
- }
- }
- }
- return list;
- }
- public CoreRow[] GetVisibleRows()
- {
- return _tree.View.Nodes.Select(x => MapRow((x.Item as CoreTreeNode)?.Row)).NotNull().ToArray();
- }
- public void InvalidateRow(CoreRow row)
- {
- if (_innerTable is null || row.Index < 0 || row.Index >= _innerTable.Rows.Count) return;
- var _innerRow = _innerTable.Rows[row.Index];
- var coreTreeNode = Nodes.Find(_innerRow);
- if(coreTreeNode is not null)
- {
- coreTreeNode.InvalidateData();
- }
- }
- public void ScrollIntoView(CoreRow row)
- {
- _tree.ScrollInView(new RowColumnIndex(row.Index + 1, 0));
- }
- public void UpdateCell(CoreRow row, string column, object? value)
- {
- throw new NotImplementedException();
- }
- public void UpdateRow(CoreRow row)
- {
- throw new NotImplementedException();
- }
- #region Drag + Drop
- private void _tree_DragOver(object sender, DragEventArgs e)
- {
- Parent.DragOver(sender, e);
- }
- private void _tree_Drop(object sender, DragEventArgs e)
- {
- Parent.Drop(sender, e);
- }
- private void RowDragDropController_DragStart(object? sender, TreeGridRowDragStartEventArgs e)
- {
- var rows = e.DraggingNodes.Select(node => MapRow((node.Item as CoreTreeNode)?.Row)).NotNull().ToArray();
- Parent.DragStart(sender, rows);
- }
- #endregion
- }
|