using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Media; using System.Windows; using Syncfusion.Windows.Tools.Controls; using Syncfusion.Data.Extensions; namespace InABox.DynamicGrid { public class MultiLookupEditorControl : DynamicEditorControl, ILookupEditorControl { private ComboBoxAdv Editor; private CoreTable LookupTable; public MultiLookupEditorControl() { OtherColumns = new Dictionary(); Lookups = new Dictionary(); Width = int.MaxValue; } public int Width { get; set; } public Dictionary Lookups { get; set; } public Dictionary OtherColumns { get; } private List> Items; public override void Configure() { base.Configure(); Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left; if (!Width.Equals(int.MaxValue)) Editor.Width = Width; Editor.IsEnabled = false; OnDefineLookups?.Invoke(this); } public void LoadLookups(CoreTable values) { var keycol = ColumnName.Split('.').Last(); //var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse()); /*foreach (var col in values.Columns) if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display")) if (!OtherColumns.ContainsKey(col.ColumnName)) OtherColumns[col.ColumnName] = string.IsNullOrWhiteSpace(prefix) ? col.ColumnName : string.Join(".", prefix, col.ColumnName);*/ var value = RetrieveValue(); //Lookups = lookups; LookupTable = values; Lookups.Clear(); Items = new List>(); //if (!IsEnumEditor()) // Editor.Items.Add(""); foreach (var row in values.Rows) { Items.Add(new(row[keycol], $"{row["Display"]}")); Lookups[row[keycol]] = string.Format("{0}", row["Display"]); } Editor.ItemsSource = Items; Editor.DisplayMemberPath = "Item2"; var sel = values.Rows.FirstOrDefault(r => r[keycol].Equals(value)); //if (IsEnumEditor()) Editor.SelectedIndex = sel != null ? sel.Index : 0; //else // Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0; //foreach (var key in lookups.Keys) // Editor.Items.Add(lookups[key]); //if ((value != null) && Lookups.ContainsKey(value)) // Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value); Editor.IsEnabled = EditorDefinition.Editable == Editable.Enabled; OnLookupsDefined?.Invoke(this); } public event OnDefineLookup OnDefineLookups; public event OnLookupsDefined OnLookupsDefined; public override int DesiredHeight() { return 25; } public override void SetFocus() { Editor.Focus(); } private bool IsEnumEditor() { return EditorDefinition is EnumLookupEditor; } public List