DFLookupControl.cs 3.6 KB

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