DFLookupControl.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using InABox.Clients;
  2. using InABox.Core;
  3. using NPOI.SS.Formula.Functions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DFLookupControl : DynamicFormFieldControl<DFLayoutLookupField, DFLayoutLookupFieldProperties, string>
  14. {
  15. private ComboBox Combo = null!; // late-initialising
  16. protected override FrameworkElement Create()
  17. {
  18. Combo = new ComboBox();
  19. var type = CoreUtils.GetEntity(Field.Properties.LookupType);
  20. if (type != null)
  21. {
  22. var client = ClientFactory.CreateClient(type);
  23. //Task.Run(() =>
  24. //{
  25. var table = client.Query(
  26. LookupFactory.DefineFilter(type),
  27. LookupFactory.DefineColumns(type),
  28. LookupFactory.DefineSort(type)
  29. );
  30. var lookups = new Dictionary<Guid, string>();
  31. foreach (var row in table.Rows)
  32. {
  33. var id = row.Get<Guid>("ID");
  34. Dictionary<string, object> values = row.ToDictionary(new[] { "ID" });
  35. var display = LookupFactory.FormatLookup(type, values, new string[] { });
  36. lookups[id] = display;
  37. }
  38. //Dispatcher.Invoke(() =>
  39. //{
  40. Combo.DisplayMemberPath = "Value";
  41. Combo.SelectedValuePath = "Key";
  42. Combo.ItemsSource = lookups;
  43. Combo.VerticalContentAlignment = VerticalAlignment.Center;
  44. Combo.SelectionChanged += (sender, e) => ChangeField();
  45. //});
  46. //});
  47. }
  48. return Combo;
  49. }
  50. public override object? GetEntityValue()
  51. {
  52. return (Guid?)Combo.SelectedValue ?? Guid.Empty;
  53. }
  54. public override void SetEntityValue(object? value)
  55. {
  56. Combo.SelectedValue = value;
  57. }
  58. public override string GetValue() => ((KeyValuePair<Guid, string>?)Combo.SelectedItem)?.Value ?? "";
  59. public override void SetValue(string? value)
  60. {
  61. var lookups = Combo.ItemsSource as Dictionary<Guid, string>;
  62. if (lookups is null)
  63. return;
  64. var lookup = lookups!.FirstOrDefault(x => x.Value == value);
  65. Combo.SelectedItem = lookup;
  66. }
  67. protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty;
  68. }
  69. }