LookupEditorControl.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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>, ILookupEditorControl
  10. {
  11. private ComboBox Editor;
  12. private CoreTable LookupTable;
  13. public LookupEditorControl()
  14. {
  15. OtherColumns = new Dictionary<string, string>();
  16. Lookups = new Dictionary<object, string>();
  17. Width = int.MaxValue;
  18. }
  19. public int Width { get; set; }
  20. public Dictionary<object, string> Lookups { get; set; }
  21. public Dictionary<string, string> OtherColumns { get; }
  22. public override void Configure()
  23. {
  24. base.Configure();
  25. Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
  26. if (!Width.Equals(int.MaxValue))
  27. Editor.Width = Width;
  28. Editor.IsEnabled = false;
  29. OnDefineLookups?.Invoke(this);
  30. }
  31. public void LoadLookups(CoreTable values)
  32. {
  33. var keycol = ColumnName.Split('.').Last();
  34. var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse());
  35. foreach (var col in values.Columns)
  36. if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display"))
  37. if (!OtherColumns.ContainsKey(col.ColumnName))
  38. OtherColumns[col.ColumnName] = string.IsNullOrWhiteSpace(prefix) ? col.ColumnName : string.Join(".", prefix, col.ColumnName);
  39. var value = RetrieveValue();
  40. //Lookups = lookups;
  41. LookupTable = values;
  42. Editor.Items.Clear();
  43. Lookups.Clear();
  44. if (!IsEnumEditor())
  45. Editor.Items.Add("");
  46. foreach (var row in values.Rows)
  47. {
  48. Editor.Items.Add(row["Display"]);
  49. Lookups[row[keycol]] = string.Format("{0}", row["Display"]);
  50. }
  51. var sel = values.Rows.FirstOrDefault(r => r[keycol].Equals(value));
  52. if (IsEnumEditor())
  53. Editor.SelectedIndex = sel != null ? sel.Index : 0;
  54. else
  55. Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0;
  56. //foreach (var key in lookups.Keys)
  57. // Editor.Items.Add(lookups[key]);
  58. //if ((value != null) && Lookups.ContainsKey(value))
  59. // Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  60. Editor.IsEnabled = EditorDefinition.Editable == Editable.Enabled;
  61. OnLookupsDefined?.Invoke(this);
  62. }
  63. public event OnDefineLookup OnDefineLookups;
  64. public event OnLookupsDefined OnLookupsDefined;
  65. public override int DesiredHeight()
  66. {
  67. return 25;
  68. }
  69. public override void SetFocus()
  70. {
  71. Editor.Focus();
  72. }
  73. private bool IsEnumEditor()
  74. {
  75. return EditorDefinition is EnumLookupEditor;
  76. }
  77. public List<Button> Buttons { get; private set; }
  78. protected override FrameworkElement CreateEditor()
  79. {
  80. var dock = new DockPanel();
  81. Buttons = CreateButtons(out var DisableEditor);
  82. foreach (var button in Buttons)
  83. {
  84. button.SetValue(DockPanel.DockProperty, Dock.Right);
  85. dock.Children.Add(button);
  86. dock.Width += button.Width + 5;
  87. }
  88. Editor = new ComboBox
  89. {
  90. VerticalAlignment = VerticalAlignment.Stretch,
  91. VerticalContentAlignment = VerticalAlignment.Center,
  92. HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left
  93. };
  94. if (!Width.Equals(int.MaxValue))
  95. Width = Width;
  96. Editor.SelectionChanged += Combobox_SelectionChanged;
  97. Editor.IsEnabled = false;
  98. if (DisableEditor)
  99. {
  100. Editor.Background = new SolidColorBrush(Colors.Silver);
  101. Editor.IsEnabled = false;
  102. }
  103. dock.Children.Add(Editor);
  104. return dock;
  105. }
  106. private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  107. {
  108. if (!Editor.IsEnabled)
  109. return;
  110. if (LookupTable != null)
  111. {
  112. var keycol = ColumnName.Split('.').Last();
  113. var row = LookupTable.Rows.FirstOrDefault(r => r[keycol].Equals(RetrieveValue()));
  114. foreach (var field in LookupTable.Columns.Where(x => OtherColumns.ContainsKey(x.ColumnName)))
  115. {
  116. var othervalue = row != null ? row[field.ColumnName] : null;
  117. OtherValues[OtherColumns[field.ColumnName]] = othervalue;
  118. }
  119. }
  120. CheckChanged();
  121. }
  122. public event OnUpdateOtherEditorHandler OnUpdateOtherEditor;
  123. public override int DesiredWidth()
  124. {
  125. return int.MaxValue;
  126. }
  127. protected override object RetrieveValue()
  128. {
  129. if (IsEnumEditor())
  130. {
  131. if (Editor.SelectedIndex >= 0 && Editor.SelectedIndex < Lookups.Keys.Count)
  132. return Lookups.Keys.ElementAt(Editor.SelectedIndex);
  133. }
  134. else
  135. {
  136. if (Editor.SelectedIndex > 0 && Editor.SelectedIndex <= Lookups.Keys.Count)
  137. return Lookups.Keys.ElementAt(Editor.SelectedIndex - 1);
  138. }
  139. return null;
  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 (Lookups.ContainsKey(value))
  161. {
  162. if (IsEnumEditor())
  163. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  164. else
  165. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value) + 1;
  166. }
  167. else
  168. {
  169. Editor.SelectedIndex = 0;
  170. }
  171. }
  172. public override void SetColor(Color color)
  173. {
  174. //Editor.Background = new SolidColorBrush(color);
  175. }
  176. public override void SetEnabled(bool enabled)
  177. {
  178. base.SetEnabled(enabled);
  179. Editor.IsEnabled = enabled;
  180. }
  181. }
  182. }