using InABox.Core; using InABox.WPF; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace InABox.DynamicGrid; public class DynamicGridColumnNameSelectorGrid : DynamicItemsListGrid, INotifyPropertyChanged { private bool _canSave; public bool CanSave { get => _canSave; set { _canSave = value; DoPropertyChanged(); } } public DynamicGridColumnNameSelectorGrid(string[] columnNames) { var items = new List(); var parentCols = new HashSet(); foreach (var column in columnNames) { var props = column.Split('.'); string? parent = null; for (int i = 0; i < props.Length - 1; ++i) { if (parent is null) { parent = props[i]; } else { parent = $"{parent}.{props[i]}"; } parentCols.Add(parent); } var item = new DynamicGridColumnNameSelectorItem { ColumnName = column, ParentColumn = parent, Display = props[^1], IsParent = false }; items.Add(item); } foreach (var col in parentCols) { var lastColIdx = col.LastIndexOf('.'); var item = new DynamicGridColumnNameSelectorItem { ColumnName = col, IsParent = true }; if (lastColIdx == -1) { item.ParentColumn = null; item.Display = col; } else { item.ParentColumn = col[..lastColIdx]; item.Display = col[(lastColIdx + 1)..]; } items.Add(item); } items.Sort((a, b) => a.ColumnName.CompareTo(b.ColumnName)); Items = items; } protected override void SelectItems(CoreRow[]? rows) { base.SelectItems(rows); CanSave = rows is not null && rows.Any(x => !LoadItem(x).IsParent); if(rows is not null) { foreach(var row in rows) { Component.ExpandRow(row); } } } protected override DynamicGridColumns LoadColumns() { var columns = new DynamicGridColumns(); columns.Add(x => x.Display, caption: "Name"); return columns; } private UIComponent? _uiComponent; private UIComponent Component { get { _uiComponent ??= new UIComponent(this); return _uiComponent; } } private class UIComponent : DynamicGridTreeUIComponent { DynamicGridColumnNameSelectorGrid Grid; public UIComponent(DynamicGridColumnNameSelectorGrid grid) : base(x => x.ColumnName, x => x.ParentColumn, null) { Parent = grid; Grid = grid; ExpandMode = DynamicTreeGridExpandMode.None; ShowHeader = false; MinRowHeight = 25; } protected override Brush? GetCellForeground(CoreRow row, DynamicColumnBase column) { var item = Grid.LoadItem(row); if (item.IsParent) { return Colors.Gray.ToBrush(); } else { return base.GetCellForeground(row, column); } } protected override FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column) { var item = Grid.LoadItem(row); if (item.IsParent) { return FontStyles.Italic; } else { return base.GetCellFontStyle(row, column); } } } protected override IDynamicGridUIComponent CreateUIComponent() => Component; public event PropertyChangedEventHandler? PropertyChanged; protected void DoPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new(propertyName)); } public static bool SelectColumnName(string[] columnNames, out string value) { var grid = new DynamicGridColumnNameSelectorGrid(columnNames); grid.Refresh(true, true); var window = new DynamicContentDialog(grid) { Title = "Select Column" }; window.Bind(DynamicContentDialog.CanSaveProperty, grid, x => x.CanSave); if(window.ShowDialog() == true && grid.SelectedRows.FirstOrDefault() is CoreRow row) { var item = grid.LoadItem(row); if (!item.IsParent) { value = item.ColumnName; return true; } } value = ""; return false; } } public class DynamicGridColumnNameSelectorItem : BaseObject { public string ColumnName { get; set; } = ""; public string Display { get; set; } = ""; public string? ParentColumn { get; set; } public bool IsParent { get; set; } }