123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using InABox.Clients;
- using InABox.Core;
- using InABox.Wpf;
- using InABox.WPF;
- using Syncfusion.Data;
- using Syncfusion.UI.Xaml.TreeGrid;
- using Syncfusion.UI.Xaml.TreeGrid.Helpers;
- namespace InABox.DynamicGrid;
- // public class CoreTreeNode : INotifyPropertyChanged
- // {
- //
- // private CoreTreeNodes _owner;
- //
- // public ObservableCollection<CoreTreeNode> Children => _owner.GetChilden(_id);
- //
- // private Guid _id;
- // public Guid ID
- // {
- // get { return _id; }
- // set
- // {
- // _id = value;
- // RaisedOnPropertyChanged("ID");
- // }
- // }
- //
- // private Guid _parent;
- // public Guid Parent
- // {
- // get { return _parent; }
- // set
- // {
- // _parent = value;
- // RaisedOnPropertyChanged("Parent");
- // }
- // }
- //
- // private string _description;
- // public string Description
- // {
- // get { return _description; }
- // set
- // {
- // _description = value;
- // RaisedOnPropertyChanged("Description");
- // }
- // }
- //
- // private ImageSource? _image;
- // public ImageSource? Image
- // {
- // get { return _image; }
- // set
- // {
- // _image = value;
- // RaisedOnPropertyChanged("Image");
- // }
- // }
- //
- // public event PropertyChangedEventHandler? PropertyChanged;
- //
- // public void RaisedOnPropertyChanged(string propertyName)
- // {
- // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- // }
- //
- // public CoreTreeNode(CoreTreeNodes owner)
- // {
- // _owner = owner;
- // _description = "";
- // }
- //
- // public CoreTreeNode(CoreTreeNodes owner, Guid id, Guid parent) : this(owner)
- // {
- // _id = id;
- // _parent = parent;
- // }
- // }
- //
- // public class CoreTreeNodes
- // {
- //
- // private List<CoreTreeNode> _nodes;
- // public ObservableCollection<CoreTreeNode> Nodes => new ObservableCollection<CoreTreeNode>(_nodes.Where(x => x.Parent == Guid.Empty));
- //
- // public CoreTreeNode? this[Guid id] => _nodes.FirstOrDefault(x => x.ID == id);
- //
- // public CoreTreeNodes()
- // {
- // _nodes = new List<CoreTreeNode>();
- // }
- //
- // public CoreTreeNode Add(Guid id, Guid parent)
- // {
- // var node = new CoreTreeNode(this, id, parent);
- // _nodes.Add(node);
- // return node;
- // }
- //
- // public void GetChildren(List<Guid> nodes, Guid id)
- // {
- // nodes.Add(id);
- // var children = GetChilden(id);
- // foreach (var child in children)
- // GetChildren(nodes, child.ID);
- // }
- //
- // public ObservableCollection<CoreTreeNode> GetChilden(Guid id)
- // {
- //
- // return new ObservableCollection<CoreTreeNode>(_nodes.Where(x => x.Parent.Equals(id) && (x.ID != id)));
- // }
- //
- // public void Load<T>(CoreTable table, Expression<Func<T, Guid>> id, Expression<Func<T, Guid>> parentid, Expression<Func<T, String>> description)
- // {
- // _nodes.Clear();
- // foreach (var row in table.Rows)
- // {
- // Guid _id = row.Get<T, Guid>(id);
- // Guid _parent = row.Get<T, Guid>(parentid);
- // String _description = row.Get<T, String>(description);
- // Add(_id, _parent).Description = _description;
- // }
- // }
- //
- // }
- public enum DynamicTreeOption
- {
- Add,
- Edit,
- Delete,
- ShowColumns
- }
- public delegate void OnSelectItem(CoreTreeNode node);
- public delegate void OnContextMenuOpening(CoreTreeNode node, ContextMenu menu);
- public abstract class DynamicTreeView<T> : ContentControl where T : BaseObject, new()
- {
- protected abstract Expression<Func<T, Guid>> ID { get; }
- protected abstract Expression<Func<T, Guid>> ParentID { get; }
- protected abstract Expression<Func<T, String>> Description { get; }
- private DynamicGridColumns MasterColumns { get; set; }
- public DynamicGridColumns VisibleColumns { get; protected set; }
- public DynamicActionColumns ActionColumns { get; }
- public DynamicGrid<T>.HiddenColumnsList HiddenColumns { get; }
- private readonly ContextMenu ColumnsMenu;
- public CoreTable Data { get; private set; }
- private ContextMenu _menu;
- private SfTreeGrid _tree;
- private DockPanel _dock;
- private Grid _grid;
- private Button _add;
- private Button _edit;
- private Button _delete;
- private Label _spacer;
- public event OnSelectItem OnSelectItem;
- public event OnContextMenuOpening OnContextMenuOpening;
- 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();
- }
- }
- private bool _shownumbers = false;
- public bool ShowNumbers
- {
- get => _shownumbers;
- set
- {
- _shownumbers = value;
- _tree.Columns[1].Width = value ? 50 : 0;
- }
- }
- /*public double RowHeight
- {
- get => _tree.RowHeight;
- set => _tree.RowHeight = value;
- }*/
-
- public DynamicTreeView() : base()
- {
-
- Options = new FluentList<DynamicTreeOption>();
- Options.OnChanged += OptionsChanged;
- MasterColumns = new DynamicGridColumns();
- MasterColumns.ExtractColumns(typeof(T));
- ColumnsMenu = new ContextMenu();
- ColumnsMenu.Opened += ColumnsMenu_ContextMenuOpening;
-
- _grid = new Grid();
- _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1D, GridUnitType.Star) });
- _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Star) });
- _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Auto) });
-
- _grid.Children.Add(_tree);
-
- _dock = new DockPanel();
- _dock.SetValue(Grid.RowProperty, 1);
- _grid.Children.Add(_dock);
- _add = CreateButton(Wpf.Resources.add.AsBitmapImage(System.Drawing.Color.White), "", "Add Item", (o) => DoAddItem(Guid.Empty, true));
- _add.Margin = new Thickness(0, 2, 2, 0);
- _add.Visibility = Visibility.Collapsed;
- _add.SetValue(DockPanel.DockProperty, Dock.Left);
- _dock.Children.Add(_add);
-
- _edit = CreateButton(Wpf.Resources.pencil.AsBitmapImage(System.Drawing.Color.White), "", "Edit Item", EditItem);
- _edit.Margin = new Thickness(0, 2, 2, 0);
- _edit.Visibility = Visibility.Collapsed;
- _edit.SetValue(DockPanel.DockProperty, Dock.Left);
- _dock.Children.Add(_edit);
- _delete = CreateButton(Wpf.Resources.delete.AsBitmapImage(System.Drawing.Color.White), "", "Delete Item", DeleteItem);
- _delete.Margin = new Thickness(2, 2, 0, 0);
- _delete.Visibility = Visibility.Collapsed;
- _delete.SetValue(DockPanel.DockProperty, Dock.Right);
- _dock.Children.Add(_delete);
-
- _spacer = new Label();
- _spacer.SetValue(DockPanel.DockProperty, Dock.Left);
- _dock.Children.Add(_spacer);
- Content = _grid;
- SizeChanged += DynamicTreeView_SizeChanged;
- }
- #region Public Interface
- public void AddItem(CoreTreeNode? parentNode = null, bool edit = true)
- {
- var id = parentNode?.ID ?? Guid.Empty;
- DoAddItem(id, edit);
-
- }
- #endregion
- 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 (Options.Contains(DynamicTreeOption.Add))
- {
- _menu.AddItem("Add Item", null, (_tree.SelectedItem as CoreTreeNode)!.ID, (id) => DoAddItem(id, true));
- }
- }
- }
- private void DynamicTreeView_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- CalculateRowHeight();
- }
- private void CalculateRowHeight()
- {
- if(Data != null && Data.Rows.Count > 0)
- {
- var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
- var targetHeight = contentHeight / Data.Rows.Count;
- _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
- }
- }
-
- private Button CreateButton(BitmapImage? image = null, string? text = null, string? tooltip = null, Action<Button>? action = null)
- {
- var button = new Button();
- button.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
- button.SetValue(BorderThicknessProperty, new Thickness(0.75));
- button.Height = 30;
- button.MinWidth = 30;
- button.Click += (o, e) => action?.Invoke(button);
- UpdateButton(button, image, text, tooltip);
- return button;
- }
- protected void UpdateButton(Button button, BitmapImage? image, string? text, string? tooltip = null)
- {
- var stackPnl = new StackPanel();
- stackPnl.Orientation = Orientation.Horizontal;
- if (image != null)
- {
- var img = new Image();
- img.Source = image;
- img.Margin = new Thickness(2);
- img.ToolTip = tooltip;
- stackPnl.Children.Add(img);
- }
- if (!string.IsNullOrEmpty(text))
- {
- var lbl = new Label();
- lbl.Content = text;
- lbl.VerticalAlignment = VerticalAlignment.Stretch;
- lbl.VerticalContentAlignment = VerticalAlignment.Center;
- lbl.Margin = new Thickness(2, 0, 5, 0);
- lbl.ToolTip = ToolTip;
- stackPnl.Children.Add(lbl);
- }
- button.Content = stackPnl;
- button.ToolTip = tooltip;
- }
- #region Options
- public FluentList<DynamicTreeOption> Options { get; private set; }
- public bool HasOption(DynamicTreeOption option, IEnumerable<DynamicTreeOption>? options = null) => (options ?? Options).Contains(option);
-
- private void OptionsChanged(object sender, EventArgs args)
- {
- _add.Visibility = HasOption(DynamicTreeOption.Add) ? Visibility.Visible : Visibility.Collapsed;
- _edit.Visibility = HasOption(DynamicTreeOption.Edit) ? Visibility.Visible : Visibility.Collapsed;
- _delete.Visibility = HasOption(DynamicTreeOption.Delete) ? Visibility.Visible : Visibility.Collapsed;
- ColumnsMenu.Visibility = HasOption(DynamicTreeOption.SelectColumns) ? Visibility.Visible : Visibility.Hidden;
- }
- #endregion
- #region CRUD
- protected virtual T DoCreateItem(Guid parent)
- {
- T result = new T();
- CoreUtils.SetPropertyValue(result, CoreUtils.GetFullPropertyName(ParentID, "."), parent);
- return result;
- }
- protected abstract T? DoLoadItem(Guid id);
- protected virtual bool DoEditItem(T item)
- {
- var form = new DynamicEditorForm(typeof(T));
- form.OnEditorValueChanged += (sender, name, value) =>
- {
- CoreUtils.SetPropertyValue(item, name, value);
- return new Dictionary<string, object?>();
- };
- form.Items = new T[] { item };
- return form.ShowDialog() == true;
- }
- protected abstract void DoSaveItem(T item);
- protected abstract bool DoDeleteItem(Guid id);
-
- protected virtual void DoAddItem(Guid id, bool edit)
- {
- try
- {
- T item = DoCreateItem(id);
- if (edit)
- {
- if (DoEditItem(item))
- {
- DoSaveItem(item);
- Refresh();
- }
- }
- else
- {
- DoSaveItem(item);
- Refresh();
- }
- }
- catch (Exception e)
- {
- MessageBox.Show(e.Message);
- }
- }
- private void EditItem(Button button)
- {
- var node = _tree.SelectedItem as CoreTreeNode;
- if (node == null)
- {
- MessageBox.Show("Please Select an item to edit!");
- return;
- }
- var item = DoLoadItem(node.ID);
- if (item != null && DoEditItem(item))
- {
- DoSaveItem(item);
- Refresh();
- }
- }
- private void DeleteItem(Button button)
- {
- var node = _tree.SelectedItem as CoreTreeNode;
- if (node == null)
- {
- MessageBox.Show("Please Select an item to edit!");
- return;
- }
- if (DoDeleteItem(node.ID))
- {
- Refresh();
- }
- }
- #endregion
- #region Header Actions
- private void ColumnsMenu_ContextMenuOpening(object sender, RoutedEventArgs e)
- {
- if (sender is not ContextMenu menu) return;
- menu.Items.Clear();
- menu.AddItem("Select Columns", null, SelectColumnsClick);
- LoadColumnsMenu(menu);
- }
- private void SelectColumnsClick()
- {
- var editor = new DynamicGridColumnsEditor(typeof(T));
- editor.Columns.AddRange(VisibleColumns);
- if (editor.ShowDialog().Equals(true))
- {
- VisibleColumns.Clear();
- VisibleColumns.AddRange(editor.Columns);
- SaveColumns(VisibleColumns);
- //OnSaveColumns?.Invoke(this, editor.Columns);
- Refresh(true, true);
- }
- }
- protected virtual void LoadColumnsMenu(ContextMenu menu)
- {
- }
- protected virtual void SaveColumns(DynamicGridColumns columns)
- {
- }
- #endregion
- #region Refresh
- private static bool IsSequenced => typeof(T).GetInterfaces().Any(x => x.Equals(typeof(ISequenceable)));
- public CoreTreeNodes Nodes { get; set; }
- protected abstract void Reload(Filters<T> criteria, Columns<T> columns, SortOrder<T>? sort, Action<CoreTable?, Exception?> action);
- private void AfterRefresh()
- {
- var nodes = new CoreTreeNodes();
- foreach (var row in Data.Rows)
- {
- var _id = row.Get(ID);
- var _parent = row.Get(ParentID);
- var _description = row.Get(Description);
- nodes.Add(_id, _parent).Description = _description;
- }
- Nodes = nodes;
- _tree.ItemsSource = nodes.Nodes;
- CalculateRowHeight();
- }
- protected virtual Filter<T>? DefineFilter()
- {
- return null;
- }
- private readonly List<DynamicColumnBase> ColumnList = new();
- private readonly Dictionary<string, string> _filterpredicates = new();
- public Columns<T> DataColumns()
- {
- var columns = new Columns<T>();
- columns.Add(ID).Add(ParentID).Add(Description);
- foreach (var column in VisibleColumns)
- columns.Add(column.ColumnName);
- foreach (var column in HiddenColumns.ColumnNames)
- columns.Add(new Column<T>(column));
- return columns;
- }
- protected virtual DynamicGridColumns LoadColumns()
- {
- var result = new DynamicGridColumns();
- var cols = new Columns<T>().Default(ColumnType.IncludeLinked, ColumnType.ExcludeID);
- result.AddRange(MasterColumns.Where(x => cols.Items.Any(c => c.Property.Equals(x.ColumnName)))
- .OrderBy(x => CoreUtils.GetPropertySequence(typeof(T), x.ColumnName)));
- return result;
- }
- private void LoadActionColumns(DynamicActionColumnPosition position)
- {
- for (var i = 0; i < ActionColumns.Count; i++)
- {
- var column = ActionColumns[i];
- if (column.Position == position)
- {
- var sColName = string.Format("ActionColumn{0}", i);
- if (column is DynamicImageColumn imgcol)
- {
- var newcol = new TreeGridImageColumn();
- newcol.MappingName = sColName;
- //newcol.Stretch = Stretch.Uniform;
- newcol.Width = column.Width == 0 ? _tree.RowHeight : column.Width;
- newcol.Padding = new Thickness(4);
- newcol.ImageHeight = _tree.RowHeight - 8;
- newcol.ImageWidth = _tree.RowHeight - 8;
- newcol.ColumnSizer = GridLengthUnitType.None;
- newcol.HeaderText = column.HeaderText;
- newcol.AllowSorting = false;
- ApplyFilterStyle(newcol, true, true);
- newcol.ShowToolTip = column.ToolTip != null;
- newcol.ShowHeaderToolTip = column.ToolTip != null;
- var style = new Style();
- style.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- style.Setters.Add(new Setter(IsEnabledProperty, false));
- newcol.FilterRowCellStyle = style;
- var headstyle = new Style(typeof(GridHeaderCellControl));
- headstyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headstyle.Setters.Add(new Setter(FontSizeProperty, 12D));
- if (!string.IsNullOrWhiteSpace(column.HeaderText))
- {
- //headstyle.Setters.Add(new Setter(LayoutTransformProperty, new RotateTransform(270.0F)));
- headstyle.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(0.0, 0.0, 0, 0)));
- headstyle.Setters.Add(new Setter(MarginProperty, new Thickness(0, 0, 0.75, 0.75)));
- if (imgcol.VerticalHeader)
- headstyle.Setters.Add(new Setter(TemplateProperty,
- Application.Current.Resources["VerticalColumnHeader"] as ControlTemplate));
- }
- else
- {
- var image = imgcol.Image?.Invoke(null);
- if (image != null)
- {
- var template = new ControlTemplate(typeof(GridHeaderCellControl));
- var border = new FrameworkElementFactory(typeof(Border));
- border.SetValue(Border.BackgroundProperty, new SolidColorBrush(Colors.Gainsboro));
- border.SetValue(Border.PaddingProperty, new Thickness(4));
- border.SetValue(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(TemplateProperty, 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(BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headstyle.Setters.Add(new Setter(FontSizeProperty, 12D));
- headstyle.Setters.Add(new Setter(MarginProperty, new Thickness(0, -0.75, 0, 0.75)));
- headstyle.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(0.75)));
- if (txtCol.VerticalHeader)
- {
- headstyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Left));
- headstyle.Setters.Add(new Setter(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 DynamicGrid<T>.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(BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headstyle.Setters.Add(new Setter(FontSizeProperty, 12D));
- headstyle.Setters.Add(new Setter(MarginProperty, new Thickness(0, -0.75, 0, 0.75)));
- headstyle.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(0.75)));
- newcol.HeaderStyle = headstyle;
- _tree.Columns.Add(newcol);
- ColumnList.Add(column);
- }
- }
- }
- }
- private void ResizeColumns(double width, double height)
- {
- if (Data == null || width <= 0)
- return;
- Dispatcher.BeginInvoke(() =>
- {
- //var vc = DataGrid.GetVisualContainer();
- //vc.RowHeightManager.Reset();
- //vc.InvalidateMeasureInfo();
- foreach (var (index, size) in DynamicGrid<T>.CalculateColumnSizes(ColumnList, width, height, _tree.RowHeight))
- _tree.Columns[index].Width = Math.Max(0.0F, size);
- });
-
- //
- // var vc = DataGrid.GetVisualContainer();
- // vc.RowHeightManager.Reset();
- // vc.InvalidateMeasureInfo();
- // if (vc.ScrollOwner != null)
- // vc.ScrollOwner.HorizontalScrollBarVisibility = vc.ExtentWidth <= fAvailWidth ? ScrollBarVisibility.Hidden : ScrollBarVisibility.Visible;
- }
- private void ReloadColumns()
- {
- VisibleColumns = LoadColumns();
- _tree.Columns.Suspend();
- ColumnList.Clear();
- _tree.Columns.Clear();
- LoadActionColumns(DynamicActionColumnPosition.Start);
- foreach (var column in VisibleColumns)
- {
- IProperty? prop;
- try
- {
- prop = DatabaseSchema.Property(typeof(T), column.ColumnName);
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, ClientFactory.UserID,
- string.Format("Error constructing Column [{0}] : {1}\n{2}", column.ColumnName, e.Message, e.StackTrace));
- prop = null;
- }
- if (prop != null)
- {
- IDynamicGridEditorColumn? newcol = null;
- if (prop.Editor is IntegerEditor)
- newcol = new DynamicGridIntegerColumn<T>(column);
- else if (prop.Editor is CurrencyEditor)
- newcol = new DynamicGridCurrencyColumn<T>(column);
- else if (prop.Editor is DoubleEditor)
- newcol = new DynamicGridDoubleColumn<T>(column);
- else if (prop.Editor is DateTimeEditor)
- newcol = new DynamicGridDateTimeColumn<T>(column);
- else if (prop.Editor is DateEditor)
- newcol = new DynamicGridDateColumn<T>(column);
- else if (prop.Editor is TimeOfDayEditor)
- newcol = new DynamicGridTimeOfDayColumn<T>(column);
- else if (prop.Editor is TimestampEditor)
- newcol = new DynamicGridTimeStampColumn<T>(column);
- else if (prop.Editor is DurationEditor)
- newcol = new DynamicGridDurationColumn<T>(column);
- else if (prop.Editor is CheckBoxEditor)
- newcol = new DynamicGridCheckBoxColumn<T>(column);
- else if (prop.Editor is ColorEditor)
- newcol = new DynamicGridColorColumn<T>(column, column.Width, (int)_tree.RowHeight);
- else if (prop.Editor is PopupEditor)
- newcol = new DynamicGridPopupColumn<T>(column);
- else if (prop.Editor is CodePopupEditor)
- newcol = new DynamicGridCodePopupColumn<T>(column);
- else if (prop.Editor is EnumLookupEditor)
- newcol = new DynamicGridEnumLookupColumn<T>(column);
- else if (prop.Editor is ComboLookupEditor)
- newcol = new DynamicGridComboLookupColumn<T>(column);
- else if (prop.Editor is LookupEditor)
- newcol = new DynamicGridLookupColumn<T>(column);
- else if (prop.Editor is MemoEditor)
- newcol = new DynamicGridMemoColumn<T>(column);
- else if (prop.Editor is CodeEditor)
- newcol = new DynamicGridCodeColumn<T>(column);
- else if (prop.Editor is UniqueCodeEditor)
- newcol = new DynamicGridUniqueCodeColumn<T>(column);
- else if (prop.Editor is TextBoxEditor)
- newcol = new DynamicGridTextBoxColumn<T>(column);
- if (newcol != null)
- {
- newcol.GetEntity = () => _editingObject.Object;
- newcol.EntityChanged += DoEntityChanged;
- if (!newcol.VariableHeight)
- gridRowResizingOptions.ExcludeColumns.Add(newcol.MappingName);
- newcol.Column.AllowEditing = newcol.Editable && IsDirectEditMode();
- var summary = newcol.Summary();
- if (summary != null)
- Summaries.Add(summary);
- ApplyFilterStyle(newcol.Column, newcol.Filtered, false);
-
- var headstyle = new Style(typeof(TreeGridHeaderCell));
- headstyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.Gainsboro)));
- headstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
- headstyle.Setters.Add(new Setter(FontSizeProperty, 12D));
- newcol.Column.HeaderStyle = headstyle;
- var cellstyle = new Style();
- if (IsDirectEditMode())
- {
- if (prop.Editor is null || !prop.Editor.Editable.IsDirectEditable())
- {
- cellstyle.Setters.Add(new Setter(BackgroundProperty,
- new SolidColorBrush(Colors.WhiteSmoke)));
- newcol.Column.AllowEditing = false;
- }
- else
- {
- cellstyle.Setters.Add(new Setter(BackgroundProperty,
- new SolidColorBrush(Colors.LightYellow)));
- newcol.Column.AllowEditing = true;
- }
- cellstyle.Setters.Add(new Setter(ForegroundProperty, new SolidColorBrush(Colors.Black)));
- newcol.Column.CellStyle = cellstyle;
- }
- else
- {
- cellstyle.Setters.Add(new Setter(BackgroundProperty,
- new Binding()
- {
- Path = new PropertyPath("."), Converter = CellBackgroundConverter,
- ConverterParameter = column.ColumnName
- }));
- cellstyle.Setters.Add(new Setter(ForegroundProperty,
- new Binding()
- { Converter = CellForegroundConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(FontSizeProperty,
- new Binding()
- { Converter = CellFontSizeConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(FontStyleProperty,
- new Binding()
- { Converter = CellFontStyleConverter, ConverterParameter = column.ColumnName }));
- cellstyle.Setters.Add(new Setter(FontWeightProperty,
- new Binding()
- { Converter = CellFontWeightConverter, ConverterParameter = column.ColumnName }));
- newcol.Column.CellStyle = cellstyle;
- }
-
- _tree.Columns.Add(newcol.Column);
- ColumnList.Add(column);
- foreach (var extra in newcol.ExtraColumns)
- AddHiddenColumn(extra);
-
- }
-
- }
- }
- 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(DataGrid, DataGrid.ActualWidth - 2, DataGrid.ActualHeight - 2);
- }
- public void Refresh(bool reloadColumns, bool reloadData)
- {
- _tree.ItemsSource = null;
- if (reloadColumns && HasOption(DynamicTreeOption.ShowColumns))
- ReloadColumns();
- if (reloadData)
- {
- var filters = new Filters<T>();
- filters.Add(DefineFilter());
- var sort = LookupFactory.DefineSort<T>();
- if (sort == null && IsSequenced)
- sort = new SortOrder<T>("Sequence");
- Reload(filters, DataColumns(), sort, (table, exception) =>
- {
- if(exception != null)
- {
- Dispatcher.Invoke(() =>
- {
- MessageWindow.ShowError("Sorry! We couldn't load the data.", exception);
- });
- }
- else if(table is not null)
- {
- Data = table;
- Dispatcher.Invoke(() =>
- {
- AfterRefresh();
- });
- }
- });
- }
- }
- #endregion
- }
|