DFLookupControl.cs 3.9 KB

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