123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 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<DynamicGridColumnNameSelectorItem>, INotifyPropertyChanged
- {
- private bool _canSave;
- public bool CanSave
- {
- get => _canSave;
- set
- {
- _canSave = value;
- DoPropertyChanged();
- }
- }
- public DynamicGridColumnNameSelectorGrid(string[] columnNames)
- {
- var items = new List<DynamicGridColumnNameSelectorItem>();
- var parentCols = new HashSet<string>();
- 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<DynamicGridColumnNameSelectorItem>();
- 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<DynamicGridColumnNameSelectorItem, string?>
- {
- 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<DynamicGridColumnNameSelectorItem> 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; }
- }
|