using InABox.Clients; using InABox.Core; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace InABox.DynamicGrid { public class DFLookupControl : DynamicFormFieldControl { private ComboBox Combo = null!; // late-initialising protected override FrameworkElement Create() { Combo = new ComboBox(); var type = CoreUtils.GetEntity(Field.Properties.LookupType); if (type != null) { var client = ClientFactory.CreateClient(type); //Task.Run(() => //{ var table = client.Query( LookupFactory.DefineFilter(type), LookupFactory.DefineColumns(type), LookupFactory.DefineSort(type) ); var lookups = new Dictionary(); foreach (var row in table.Rows) { var id = row.Get("ID"); Dictionary values = row.ToDictionary(new[] { "ID" }); var display = LookupFactory.FormatLookup(type, values, new string[] { }); lookups[id] = display; } //Dispatcher.Invoke(() => //{ Combo.DisplayMemberPath = "Value"; Combo.SelectedValuePath = "Key"; Combo.ItemsSource = lookups; Combo.VerticalContentAlignment = VerticalAlignment.Center; Combo.SelectionChanged += (sender, e) => ChangeField(); //}); //}); } return Combo; } public override object? GetEntityValue() { return (Guid?)Combo.SelectedValue ?? Guid.Empty; } public override void SetEntityValue(object? value) { Combo.SelectedValue = value; } public override string GetValue() => ((KeyValuePair?)Combo.SelectedItem)?.Value ?? ""; public override void SetValue(string? value) { var lookups = Combo.ItemsSource as Dictionary; if (lookups is null) return; var lookup = lookups!.FirstOrDefault(x => x.Value == value); Combo.SelectedItem = lookup; } protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty; } }