MultiLookupEditorControl.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using Syncfusion.Windows.Tools.Controls;
  11. using Syncfusion.Data.Extensions;
  12. namespace InABox.DynamicGrid
  13. {
  14. public class MultiLookupEditorControl : DynamicEditorControl<object?>, ILookupEditorControl
  15. {
  16. private ComboBoxAdv Editor;
  17. public MultiLookupEditorControl()
  18. {
  19. OtherColumns = new Dictionary<string, string>();
  20. Lookups = new Dictionary<object, string>();
  21. Width = int.MaxValue;
  22. }
  23. public int Width { get; set; }
  24. public Dictionary<object, string> Lookups { get; set; }
  25. public Dictionary<string, string> OtherColumns { get; }
  26. private List<Tuple<object?, string>> Items;
  27. public override void Configure()
  28. {
  29. base.Configure();
  30. Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
  31. if (!Width.Equals(int.MaxValue))
  32. Editor.Width = Width;
  33. Editor.IsEnabled = false;
  34. OnDefineLookups?.Invoke(this);
  35. }
  36. public void LoadLookups(CoreTable values)
  37. {
  38. var keycol = ColumnName.Split('.').Last();
  39. //var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse());
  40. /*foreach (var col in values.Columns)
  41. if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display"))
  42. if (!OtherColumns.ContainsKey(col.ColumnName))
  43. OtherColumns[col.ColumnName] = string.IsNullOrWhiteSpace(prefix) ? col.ColumnName : string.Join(".", prefix, col.ColumnName);*/
  44. var value = RetrieveValue();
  45. //Lookups = lookups;
  46. Lookups.Clear();
  47. Items = new List<Tuple<object?, string>>();
  48. //if (!IsEnumEditor())
  49. // Editor.Items.Add("");
  50. var selected = Editor.SelectedItems is not null ? Editor.SelectedItems.Cast<Tuple<object?, string>>().Select(x => x.Item1) : Enumerable.Empty<object?>();
  51. foreach (var row in values.Rows)
  52. {
  53. var key = row[keycol];
  54. Items.Add(new(key, $"{row["Display"]}"));
  55. if (key != null)
  56. {
  57. Lookups[key] = string.Format("{0}", row["Display"]);
  58. }
  59. }
  60. Editor.ItemsSource = Items;
  61. Editor.DisplayMemberPath = "Item2";
  62. Editor.SelectedItems = Items.Where(x => selected.Contains(x.Item1)).ToObservableCollection();
  63. //var sel = values.Rows.FirstOrDefault(r => r[keycol].Equals(value));
  64. //if(sel is null)
  65. //if (IsEnumEditor())
  66. // Editor.SelectedIndex = sel != null ? sel.Index : 0;
  67. //else
  68. // Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0;
  69. //foreach (var key in lookups.Keys)
  70. // Editor.Items.Add(lookups[key]);
  71. //if ((value != null) && Lookups.ContainsKey(value))
  72. // Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  73. Editor.IsEnabled = EditorDefinition.Editable == Editable.Enabled;
  74. OnLookupsDefined?.Invoke(this);
  75. }
  76. public event OnDefineLookup OnDefineLookups;
  77. public event OnLookupsDefined OnLookupsDefined;
  78. public override int DesiredHeight()
  79. {
  80. return 25;
  81. }
  82. public override void SetFocus()
  83. {
  84. Editor.Focus();
  85. }
  86. private bool IsEnumEditor()
  87. {
  88. return EditorDefinition is EnumLookupEditor;
  89. }
  90. public List<Button> Buttons { get; private set; }
  91. protected override FrameworkElement CreateEditor()
  92. {
  93. var dock = new DockPanel();
  94. Buttons = CreateButtons(out var DisableEditor);
  95. foreach (var button in Buttons)
  96. {
  97. button.SetValue(DockPanel.DockProperty, Dock.Right);
  98. dock.Children.Add(button);
  99. dock.Width += button.Width + 5;
  100. }
  101. Editor = new ComboBoxAdv
  102. {
  103. VerticalAlignment = VerticalAlignment.Stretch,
  104. VerticalContentAlignment = VerticalAlignment.Center,
  105. HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left,
  106. IsEditable = false,
  107. AllowMultiSelect = true
  108. };
  109. Editor.SelectionChanged += Combobox_SelectionChanged;
  110. Editor.IsEnabled = false;
  111. if (DisableEditor)
  112. {
  113. Editor.Background = new SolidColorBrush(Colors.Silver);
  114. Editor.IsEnabled = false;
  115. }
  116. dock.Children.Add(Editor);
  117. return dock;
  118. }
  119. private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  120. {
  121. if (!Editor.IsEnabled)
  122. return;
  123. /*if (LookupTable != null)
  124. {
  125. var keycol = ColumnName.Split('.').Last();
  126. var row = LookupTable.Rows.FirstOrDefault(r => object.Equals(r[keycol], RetrieveValue()));
  127. foreach (var field in LookupTable.Columns.Where(x => OtherColumns.ContainsKey(x.ColumnName)))
  128. {
  129. var othervalue = row?[field.ColumnName];
  130. OtherValues[OtherColumns[field.ColumnName]] = othervalue;
  131. }
  132. }*/
  133. CheckChanged();
  134. }
  135. public event OnUpdateOtherEditorHandler OnUpdateOtherEditor;
  136. public override int DesiredWidth()
  137. {
  138. return int.MaxValue;
  139. }
  140. protected override object? RetrieveValue()
  141. {
  142. if (Editor.SelectedIndex >= 0 && Editor.SelectedIndex < Lookups.Keys.Count)
  143. return string.Join(',', Editor.SelectedItems.Cast<Tuple<object?, string>>().Select(x => x.Item1));
  144. return "";
  145. }
  146. protected override void UpdateValue(object? value)
  147. {
  148. if (Editor == null)
  149. return;
  150. if (value == null)
  151. {
  152. Editor.SelectedItem = null;
  153. return;
  154. }
  155. if (!Loaded && !Editor.IsEnabled)
  156. {
  157. Lookups.Clear();
  158. Lookups[value] = "Loading";
  159. Editor.Items.Clear();
  160. Editor.Items.Add("");
  161. Editor.Items.Add("Loading...");
  162. Editor.SelectedIndex = IsEnumEditor() ? 0 : 1;
  163. return;
  164. }
  165. if(value is not string str)
  166. {
  167. return;
  168. }
  169. IEnumerable<Tuple<object?, string>> selected;
  170. if (!string.IsNullOrWhiteSpace(str))
  171. {
  172. var values = str.Split(',');
  173. selected = values.Select(x => Items.FirstOrDefault(y => y.Item1?.ToString() == x)).Where(x => x is not null)!;
  174. }
  175. else
  176. {
  177. selected = Enumerable.Empty<Tuple<object?, string>>();
  178. }
  179. Editor.SelectedItems = selected.ToObservableCollection();
  180. /*if (Lookups.ContainsKey(value))
  181. {
  182. if (IsEnumEditor())
  183. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  184. else
  185. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value) + 1;
  186. }
  187. else
  188. {
  189. Editor.SelectedIndex = 0;
  190. }*/
  191. }
  192. public override void SetColor(Color color)
  193. {
  194. //Editor.Background = new SolidColorBrush(color);
  195. }
  196. public override void SetEnabled(bool enabled)
  197. {
  198. base.SetEnabled(enabled);
  199. Editor.IsEnabled = enabled;
  200. }
  201. }
  202. }