using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using InABox.Core; using InABox.Wpf; namespace InABox.DynamicGrid { public partial class PopupList : ThemableWindow { private readonly Dictionary _filters; private IDynamicGrid _grid; private readonly string[] _othercolumns = Array.Empty(); private readonly Type _type; public PopupList(Type type, Guid id, string[] OtherColumns, Dictionary Filters = null) { InitializeComponent(); OtherValues = new Dictionary(); _filters = Filters; _othercolumns = OtherColumns; _type = type; ID = id; SourceInitialized += PopupList_SourceInitialized; } public Guid ID { get; set; } public Dictionary OtherValues { get; } public string AsLookup() { var result = new List(); foreach (var key in OtherValues.Keys.ToArray()) { var comps = key.Split(new[] { "->" }, StringSplitOptions.None); result.Add(_grid.SelectedRows.First().Get(comps.First())); } return string.Join(" / ", result.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString()))); } public event OnDefineFilter? OnDefineFilter; private void PopupList_SourceInitialized(object? sender, EventArgs e) { //_grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicDataGrid<>), _type); _grid = (Activator.CreateInstance(typeof(DynamicDataGrid<>).MakeGenericType(_type)) as IDynamicGrid)!; _grid.Margin = new Thickness(5, 5, 5, 0); ((DependencyObject)_grid).SetValue(Grid.ColumnProperty, 0); ((DependencyObject)_grid).SetValue(Grid.ColumnSpanProperty, 3); ((DependencyObject)_grid).SetValue(Grid.RowProperty, 0); //_grid.AddHiddenColumn("AsLookup"); foreach (var column in _othercolumns) { var comps = column.Split(new[] { "->" }, StringSplitOptions.None); _grid.AddHiddenColumn(comps.First()); OtherValues[column] = null; } layoutGrid.Children.Add((UIElement)_grid); _grid.Options.BeginUpdate().Clear().AddRange( DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows ).EndUpdate(); CoreUtils.SetPropertyValue(_grid, "ColumnsTag", "Popup"); _grid.OnDefineFilter += (control, type) => { return OnDefineFilter?.Invoke(control, type); }; _grid.Refresh(true, false); if (_filters != null) foreach (var key in _filters.Keys) { // TODO: Get this working again //_grid.AddVisualFilter(key, _filters[key]); } _grid.Refresh(false, true); var screen = WpfScreen.GetScreenFrom(this); var maxwidth = (int)screen.WorkingArea.Width - 200; var DesiredWidth = _grid.DesiredWidth(); if (DesiredWidth > maxwidth) DesiredWidth = maxwidth; if (DesiredWidth > Width) Width = DesiredWidth; var maxheight = (int)screen.WorkingArea.Height - 200; var DesiredHeight = (int)(Width * 0.6); if (DesiredHeight > maxheight) DesiredHeight = maxheight; if (DesiredHeight > Height) Height = DesiredHeight; _grid.SelectedRows = _grid.Data.Rows.Where(r => r.Get("ID").Equals(ID)).ToArray(); //Left = screen.DeviceBounds.Left + ((screen.DeviceBounds.Width - Width) / 2.0F); //Top = screen.DeviceBounds.Top + ((screen.DeviceBounds.Height - Height) / 2.0F); //WpfScreen.CenterWindowOnScreen(this); } private void OKButton_Click(object sender, RoutedEventArgs e) { if (_grid.SelectedRows.Any()) { ID = _grid.SelectedRows.First().Get("ID"); //AsLookup = _grid.SelectedRows.First().Get("AsLookup"); foreach (var key in OtherValues.Keys.ToArray()) { var comps = key.Split(new[] { "->" }, StringSplitOptions.None); OtherValues[key] = _grid.SelectedRows.First().Get(comps.First()); } DialogResult = true; Close(); } else { MessageBox.Show("Please select an Item first"); } } private void CancelButton_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } } }