BaseDynamicEditorControl.cs 7.0 KB

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