LookupEditorControl.cs 7.6 KB

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