DFLookupControl.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. using ComboItemType = System.Tuple<string, System.Collections.Generic.Dictionary<string, object>>;
  12. namespace InABox.DynamicGrid
  13. {
  14. public class DFLookupControl : DynamicFormFieldControl<DFLayoutLookupField, DFLayoutLookupFieldProperties, string>
  15. {
  16. private ComboBox Combo = null!; // late-initialising
  17. protected override FrameworkElement Create()
  18. {
  19. Combo = new ComboBox();
  20. if (CoreUtils.TryGetEntity(Field.Properties.LookupType, out var type))
  21. {
  22. var client = ClientFactory.CreateClient(type);
  23. //Task.Run(() =>
  24. //{
  25. var columns = LookupFactory.DefineColumns(type);
  26. foreach(var property in Field.Properties.AdditionalPropertiesList)
  27. {
  28. columns.Add(property);
  29. }
  30. var table = client.Query(
  31. LookupFactory.DefineFilter(type),
  32. columns,
  33. LookupFactory.DefineSort(type)
  34. );
  35. var lookups = new Dictionary<Guid, ComboItemType>();
  36. foreach (var row in table.Rows)
  37. {
  38. var id = row.Get<Guid>("ID");
  39. var values = row.ToDictionary(new[] { "ID" });
  40. var display = LookupFactory.FormatLookup(type, values, new string[] { });
  41. lookups[id] = new(display, values);
  42. }
  43. //Dispatcher.Invoke(() =>
  44. //{
  45. Combo.DisplayMemberPath = "Value.Item1";
  46. Combo.SelectedValuePath = "Key";
  47. Combo.ItemsSource = lookups;
  48. Combo.VerticalContentAlignment = VerticalAlignment.Center;
  49. Combo.SelectionChanged += (sender, e) => ChangeField();
  50. //});
  51. //});
  52. }
  53. return Combo;
  54. }
  55. public override object? GetEntityValue()
  56. {
  57. return (Guid?)Combo.SelectedValue ?? Guid.Empty;
  58. }
  59. public override void SetEntityValue(object? value)
  60. {
  61. Combo.SelectedValue = value;
  62. }
  63. public override object? GetData(string dataField)
  64. {
  65. return ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Value.Item2.GetValueOrDefault(dataField);
  66. }
  67. public override Dictionary<string, object?>? GetAdditionalValues()
  68. {
  69. if (Field.Properties.AdditionalPropertiesList.Count == 0)
  70. return null;
  71. var values = new Dictionary<string, object?>();
  72. foreach(var field in Field.Properties.AdditionalPropertiesList)
  73. {
  74. values[field] = GetData(field);
  75. }
  76. return values;
  77. }
  78. public override string GetValue() => ((KeyValuePair<Guid, ComboItemType>?)Combo.SelectedItem)?.Value.Item1 ?? "";
  79. public override void SetValue(string? value)
  80. {
  81. var lookups = Combo.ItemsSource as Dictionary<Guid, ComboItemType>;
  82. if (lookups is null)
  83. return;
  84. var lookup = lookups!.FirstOrDefault(x => x.Value.Item1 == value);
  85. Combo.SelectedItem = lookup;
  86. }
  87. protected override bool IsEmpty() => Combo.SelectedValue == null || (Guid)Combo.SelectedValue == Guid.Empty;
  88. }
  89. }