DynamicEditorControl.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Core;
  8. namespace InABox.DynamicGrid
  9. {
  10. public delegate void EditorControlValueChangedHandler(IDynamicEditorControl sender, Dictionary<string, object> values);
  11. public interface IDynamicEditorControl
  12. {
  13. string ColumnName { get; set; }
  14. bool Loaded { get; set; }
  15. BaseEditor EditorDefinition { get; set; }
  16. bool IsEnabled { get; set; }
  17. void SetFocus();
  18. int DesiredHeight();
  19. event EditorControlValueChangedHandler OnEditorValueChanged;
  20. void Configure();
  21. void SetEnabled(bool enabled);
  22. void SetVisible(bool enabled);
  23. void SetValue(object value);
  24. object GetValue();
  25. }
  26. public interface ILookupEditorControl : IDynamicEditorControl
  27. {
  28. Dictionary<string, string> OtherColumns { get; }
  29. event OnDefineLookup OnDefineLookups;
  30. event OnLookupsDefined OnLookupsDefined;
  31. void LoadLookups(CoreTable values);
  32. }
  33. public interface IPopupEditorControl
  34. {
  35. event OnDefineFilter OnDefineFilter;
  36. }
  37. public abstract class BaseDynamicEditorControl : ContentControl, IDynamicEditorControl
  38. {
  39. public static readonly DependencyProperty ColumnNameProperty =
  40. DependencyProperty.Register(nameof(ColumnName), typeof(string), typeof(BaseDynamicEditorControl));
  41. public static readonly DependencyProperty ColorProperty =
  42. DependencyProperty.Register("Color", typeof(Color), typeof(BaseDynamicEditorControl));
  43. public new static readonly DependencyProperty IsEnabledProperty =
  44. DependencyProperty.Register(nameof(IsEnabled), typeof(bool), typeof(BaseDynamicEditorControl));
  45. private BaseEditor _editordefinition;
  46. public BaseDynamicEditorControl()
  47. {
  48. Color = Colors.LightYellow;
  49. }
  50. public Color Color
  51. {
  52. get => (Color)GetValue(ColorProperty);
  53. set
  54. {
  55. SetValue(ColorProperty, value);
  56. if (EditorDefinition != null)
  57. SetColor(Color);
  58. }
  59. }
  60. public string ColumnName
  61. {
  62. get => (string)GetValue(ColumnNameProperty);
  63. set => SetValue(ColumnNameProperty, value);
  64. }
  65. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  66. public BaseEditor EditorDefinition
  67. {
  68. get => _editordefinition;
  69. set
  70. {
  71. _editordefinition = value;
  72. Content = CreateEditor();
  73. SetColor(Color);
  74. }
  75. }
  76. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  77. public bool Loaded { get; set; }
  78. public virtual event EditorControlValueChangedHandler OnEditorValueChanged;
  79. public abstract int DesiredHeight();
  80. public abstract void SetFocus();
  81. public virtual void Configure()
  82. {
  83. }
  84. public abstract void SetEnabled(bool enabled);
  85. public abstract void SetVisible(bool visible);
  86. public abstract void SetValue(object value);
  87. public abstract object GetValue();
  88. public new bool IsEnabled
  89. {
  90. get => (bool)GetValue(IsEnabledProperty);
  91. set
  92. {
  93. SetValue(IsEnabledProperty, value);
  94. SetEnabled(value);
  95. }
  96. }
  97. protected abstract FrameworkElement CreateEditor();
  98. public abstract int DesiredWidth();
  99. public abstract void SetColor(Color color);
  100. protected List<Button> CreateButtons(out bool bDisableEditor)
  101. {
  102. var buttons = new List<Button>();
  103. bDisableEditor = false;
  104. if (EditorDefinition != null && EditorDefinition is IButtonEditor)
  105. {
  106. var ce = (IButtonEditor)EditorDefinition;
  107. if (ce.Buttons != null)
  108. foreach (var ceb in ce.Buttons.Reverse())
  109. {
  110. if (ceb.DisableEditor)
  111. bDisableEditor = true;
  112. var button = new Button
  113. {
  114. HorizontalAlignment = HorizontalAlignment.Left,
  115. VerticalAlignment = VerticalAlignment.Stretch,
  116. VerticalContentAlignment = VerticalAlignment.Center,
  117. HorizontalContentAlignment = HorizontalAlignment.Center,
  118. Content = ceb.Caption,
  119. Width = ceb.Width,
  120. Margin = new Thickness(5, 0, 0, 0),
  121. Padding = new Thickness(5, 0, 5, 0),
  122. Tag = ceb,
  123. IsEnabled = ceb.IsEnabled
  124. };
  125. button.Click += (o, e) =>
  126. {
  127. var b = (Button)o;
  128. var eb = b.Tag as EditorButton;
  129. eb.Click(this);
  130. };
  131. ceb.OnEnabled += (enabled) =>
  132. {
  133. button.IsEnabled = enabled;
  134. };
  135. buttons.Add(button);
  136. }
  137. }
  138. return buttons;
  139. }
  140. }
  141. public abstract class DynamicEditorControl<T> : BaseDynamicEditorControl
  142. {
  143. //{
  144. // get { return (EditorControlValueChangedHandler)GetValue(OnEditorValueChangedProperty); }
  145. // set { SetValue(OnEditorValueChangedProperty, value) }
  146. //}
  147. protected bool _changed;
  148. private bool Updating;
  149. public DynamicEditorControl()
  150. {
  151. Loaded = false;
  152. OtherValues = new Dictionary<string, object>();
  153. MinHeight = 25;
  154. Focusable = false;
  155. }
  156. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  157. public bool Changed
  158. {
  159. get => _changed;
  160. set => _changed = value;
  161. }
  162. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  163. public T Value
  164. {
  165. get => RetrieveValue();
  166. set => UpdateValue(value);
  167. }
  168. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  169. protected virtual Dictionary<string, object> OtherValues { get; }
  170. //public static readonly DependencyProperty OnEditorValueChangedProperty =
  171. // DependencyProperty.Register(nameof(OnEditorValueChanged), typeof(EditorControlValueChangedHandler), typeof(DynamicEditorControl<T>));
  172. public override event EditorControlValueChangedHandler OnEditorValueChanged;
  173. protected bool CheckChanged()
  174. {
  175. //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
  176. if (Loaded && !Updating)
  177. {
  178. Updating = true;
  179. try
  180. {
  181. var values = new Dictionary<string, object>();
  182. var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
  183. values[sColumn] = RetrieveValue();
  184. foreach (var key in OtherValues.Keys)
  185. values[key] = OtherValues[key];
  186. OnEditorValueChanged?.Invoke(this, values);
  187. }
  188. finally
  189. {
  190. Changed = true;
  191. Updating = false;
  192. }
  193. }
  194. return Changed;
  195. }
  196. public override void SetValue(object value)
  197. {
  198. UpdateValue(value != null ? (T)value : default);
  199. }
  200. public override object GetValue()
  201. {
  202. return RetrieveValue();
  203. }
  204. protected abstract T RetrieveValue();
  205. protected abstract void UpdateValue(T value);
  206. public override bool ShouldSerializeContent()
  207. {
  208. return false;
  209. }
  210. public override void SetEnabled(bool enabled)
  211. {
  212. var element = Content as FrameworkElement;
  213. element.IsEnabled = enabled;
  214. SetColor(enabled ? Color : Colors.WhiteSmoke);
  215. }
  216. public override void SetVisible(bool visible)
  217. {
  218. Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  219. }
  220. }
  221. }