BaseDynamicEditorControl.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using RoslynPad.Editor;
  10. namespace InABox.DynamicGrid
  11. {
  12. public abstract class BaseDynamicEditorControl : ContentControl, IDynamicEditorControl
  13. {
  14. public static readonly DependencyProperty ColumnNameProperty =
  15. DependencyProperty.Register(nameof(ColumnName), typeof(string), typeof(BaseDynamicEditorControl));
  16. public static readonly DependencyProperty ColorProperty =
  17. DependencyProperty.Register("Color", typeof(Color), typeof(BaseDynamicEditorControl));
  18. public new static readonly DependencyProperty IsEnabledProperty =
  19. DependencyProperty.Register(nameof(IsEnabled), typeof(bool), typeof(BaseDynamicEditorControl));
  20. private IBaseEditor _editordefinition;
  21. public BaseDynamicEditorControl()
  22. {
  23. Color = Colors.LightYellow;
  24. }
  25. public Color Color
  26. {
  27. get => (Color)GetValue(ColorProperty);
  28. set
  29. {
  30. SetValue(ColorProperty, value);
  31. if (EditorDefinition != null)
  32. SetColor(Color);
  33. }
  34. }
  35. public string ColumnName
  36. {
  37. get => (string)GetValue(ColumnNameProperty);
  38. set => SetValue(ColumnNameProperty, value);
  39. }
  40. protected TextBox? Information { get; private set; }
  41. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  42. public IBaseEditor EditorDefinition
  43. {
  44. get => _editordefinition;
  45. set
  46. {
  47. _editordefinition = value;
  48. if (_editordefinition.Information != null)
  49. {
  50. DockPanel _dock = new DockPanel();
  51. var _editor = CreateEditor();
  52. _editor.SetValue(DockPanel.DockProperty, Dock.Left);
  53. _dock.Children.Add(_editor);
  54. Information = new TextBox()
  55. {
  56. VerticalAlignment = VerticalAlignment.Stretch,
  57. VerticalContentAlignment = VerticalAlignment.Center,
  58. HorizontalAlignment = HorizontalAlignment.Stretch,
  59. Margin = new Thickness(5, 0, 0, 0),
  60. IsReadOnly = true,
  61. Focusable = false,
  62. Background = new SolidColorBrush(Colors.WhiteSmoke),
  63. BorderBrush = new SolidColorBrush(Colors.Silver)
  64. };
  65. Information.SetValue(DockPanel.DockProperty, Dock.Left);
  66. _dock.Children.Add(Information);
  67. Content = _dock;
  68. }
  69. else
  70. Content = CreateEditor();
  71. SetColor(Color);
  72. }
  73. }
  74. protected void UpdateInformation()
  75. {
  76. if (EditorDefinition?.Information != null && Information != null)
  77. {
  78. var _info = Activator.CreateInstance(EditorDefinition.Information) as IEditorInformation;
  79. Information.Text = _info?.Display(Host.GetItems()) ?? "";
  80. }
  81. }
  82. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  83. public bool Loaded { get; set; }
  84. public abstract bool Changed { get; set; }
  85. public virtual event EditorControlValueChangedHandler OnEditorValueChanged;
  86. #region Abstract Functions
  87. public abstract void Configure();
  88. protected abstract FrameworkElement CreateEditor();
  89. public abstract int DesiredWidth();
  90. public abstract int DesiredHeight();
  91. public abstract void SetColor(Color color);
  92. public abstract void SetFocus();
  93. public abstract void SetEnabled(bool enabled);
  94. public abstract void SetVisible(bool visible);
  95. #endregion
  96. public virtual void SetValue(string property, object? value)
  97. {
  98. UpdateInformation();
  99. }
  100. /// <summary>
  101. ///
  102. /// </summary>
  103. /// <param name="property">The full property name of the entity being edited.</param>
  104. /// <returns></returns>
  105. public abstract object? GetValue(string property);
  106. public abstract Dictionary<string, object?> GetValues();
  107. public new bool IsEnabled
  108. {
  109. get => (bool)GetValue(IsEnabledProperty);
  110. set
  111. {
  112. SetValue(IsEnabledProperty, value);
  113. SetEnabled(value);
  114. }
  115. }
  116. public IDynamicEditorHost Host { get; set; }
  117. protected List<Button> CreateButtons(out bool bDisableEditor)
  118. {
  119. var result = new List<Button>();
  120. bDisableEditor = false;
  121. if (EditorDefinition != null && EditorDefinition is IButtonEditor ce && ce.Buttons is not null)
  122. {
  123. var generator = Activator.CreateInstance(ce.Buttons) as IButtonGenerator;
  124. var buttons = generator?.Generate();
  125. foreach (var ceb in buttons.Reverse())
  126. {
  127. if (ceb.DisableEditor)
  128. bDisableEditor = true;
  129. var button = new Button
  130. {
  131. HorizontalAlignment = HorizontalAlignment.Left,
  132. VerticalAlignment = VerticalAlignment.Stretch,
  133. VerticalContentAlignment = VerticalAlignment.Center,
  134. HorizontalContentAlignment = HorizontalAlignment.Center,
  135. Content = ceb.Caption,
  136. Width = ceb.Width,
  137. Margin = new Thickness(5, 0, 0, 0),
  138. Padding = new Thickness(5, 0, 5, 0),
  139. Tag = ceb,
  140. IsEnabled = ceb.IsEnabled,
  141. Visibility = ceb.IsVisible ? Visibility.Visible : Visibility.Collapsed
  142. };
  143. button.Click += (o, e) =>
  144. {
  145. Host.EditorButtonClicked(EditorDefinition);
  146. // var b = (Button)o;
  147. // var eb = b.Tag as EditorButton;
  148. // eb.Click(this);
  149. };
  150. ceb.OnVisible += (visible) =>
  151. {
  152. button.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  153. };
  154. ceb.OnEnabled += (enabled) =>
  155. {
  156. button.IsEnabled = enabled;
  157. };
  158. result.Add(button);
  159. }
  160. }
  161. return result;
  162. }
  163. }
  164. public abstract class BaseDynamicEditorControl<TEditor> : BaseDynamicEditorControl
  165. where TEditor : IBaseEditor
  166. {
  167. public new TEditor EditorDefinition
  168. {
  169. get => (TEditor)base.EditorDefinition;
  170. set
  171. {
  172. base.EditorDefinition = value;
  173. }
  174. }
  175. }
  176. }