MultiLookupEditorControl.cs 7.7 KB

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