BaseDynamicEditorControl.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. public abstract int DesiredHeight();
  87. public abstract void SetFocus();
  88. public abstract void Configure();
  89. public abstract void SetEnabled(bool enabled);
  90. public abstract void SetVisible(bool visible);
  91. public virtual void SetValue(string property, object? value)
  92. {
  93. UpdateInformation();
  94. }
  95. /// <summary>
  96. ///
  97. /// </summary>
  98. /// <param name="property">The full property name of the entity being edited.</param>
  99. /// <returns></returns>
  100. public abstract object? GetValue(string property);
  101. public abstract Dictionary<string, object?> GetValues();
  102. public new bool IsEnabled
  103. {
  104. get => (bool)GetValue(IsEnabledProperty);
  105. set
  106. {
  107. SetValue(IsEnabledProperty, value);
  108. SetEnabled(value);
  109. }
  110. }
  111. protected abstract FrameworkElement CreateEditor();
  112. public abstract int DesiredWidth();
  113. public abstract void SetColor(Color color);
  114. public IDynamicEditorHost Host { get; set; }
  115. protected List<Button> CreateButtons(out bool bDisableEditor)
  116. {
  117. var buttons = new List<Button>();
  118. bDisableEditor = false;
  119. if (EditorDefinition != null && EditorDefinition is IButtonEditor ce && ce.Buttons is not null)
  120. {
  121. foreach (var ceb in ce.Buttons.Reverse())
  122. {
  123. if (ceb.DisableEditor)
  124. bDisableEditor = true;
  125. var button = new Button
  126. {
  127. HorizontalAlignment = HorizontalAlignment.Left,
  128. VerticalAlignment = VerticalAlignment.Stretch,
  129. VerticalContentAlignment = VerticalAlignment.Center,
  130. HorizontalContentAlignment = HorizontalAlignment.Center,
  131. Content = ceb.Caption,
  132. Width = ceb.Width,
  133. Margin = new Thickness(5, 0, 0, 0),
  134. Padding = new Thickness(5, 0, 5, 0),
  135. Tag = ceb,
  136. IsEnabled = ceb.IsEnabled,
  137. Visibility = ceb.IsVisible ? Visibility.Visible : Visibility.Collapsed
  138. };
  139. button.Click += (o, e) =>
  140. {
  141. var b = (Button)o;
  142. var eb = b.Tag as EditorButton;
  143. eb.Click(this);
  144. };
  145. ceb.OnVisible += (visible) =>
  146. {
  147. button.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  148. };
  149. ceb.OnEnabled += (enabled) =>
  150. {
  151. button.IsEnabled = enabled;
  152. };
  153. buttons.Add(button);
  154. }
  155. }
  156. return buttons;
  157. }
  158. }
  159. public abstract class BaseDynamicEditorControl<TEditor> : BaseDynamicEditorControl
  160. where TEditor : IBaseEditor
  161. {
  162. public new TEditor EditorDefinition
  163. {
  164. get => (TEditor)base.EditorDefinition;
  165. set
  166. {
  167. base.EditorDefinition = value;
  168. }
  169. }
  170. }
  171. }