MultiLookupEditorControl.cs 8.0 KB

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