BaseDynamicEditorControl.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 abstract class BaseDynamicEditorControl : ContentControl, IDynamicEditorControl
  11. {
  12. public static readonly DependencyProperty ColumnNameProperty =
  13. DependencyProperty.Register(nameof(ColumnName), typeof(string), typeof(BaseDynamicEditorControl));
  14. public static readonly DependencyProperty ColorProperty =
  15. DependencyProperty.Register("Color", typeof(Color), typeof(BaseDynamicEditorControl));
  16. public new static readonly DependencyProperty IsEnabledProperty =
  17. DependencyProperty.Register(nameof(IsEnabled), typeof(bool), typeof(BaseDynamicEditorControl));
  18. private IBaseEditor _editordefinition;
  19. public BaseDynamicEditorControl()
  20. {
  21. Color = Colors.LightYellow;
  22. }
  23. public Color Color
  24. {
  25. get => (Color)GetValue(ColorProperty);
  26. set
  27. {
  28. SetValue(ColorProperty, value);
  29. if (EditorDefinition != null)
  30. SetColor(Color);
  31. }
  32. }
  33. public string ColumnName
  34. {
  35. get => (string)GetValue(ColumnNameProperty);
  36. set => SetValue(ColumnNameProperty, value);
  37. }
  38. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  39. public IBaseEditor EditorDefinition
  40. {
  41. get => _editordefinition;
  42. set
  43. {
  44. _editordefinition = value;
  45. Content = CreateEditor();
  46. SetColor(Color);
  47. }
  48. }
  49. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  50. public bool Loaded { get; set; }
  51. public abstract bool Changed { get; set; }
  52. public virtual event EditorControlValueChangedHandler OnEditorValueChanged;
  53. public abstract int DesiredHeight();
  54. public abstract void SetFocus();
  55. public abstract void Configure();
  56. public abstract void SetEnabled(bool enabled);
  57. public abstract void SetVisible(bool visible);
  58. public abstract void SetValue(string property, object? value);
  59. /// <summary>
  60. ///
  61. /// </summary>
  62. /// <param name="property">The full property name of the entity being edited.</param>
  63. /// <returns></returns>
  64. public abstract object? GetValue(string property);
  65. public abstract Dictionary<string, object?> GetValues();
  66. public new bool IsEnabled
  67. {
  68. get => (bool)GetValue(IsEnabledProperty);
  69. set
  70. {
  71. SetValue(IsEnabledProperty, value);
  72. SetEnabled(value);
  73. }
  74. }
  75. protected abstract FrameworkElement CreateEditor();
  76. public abstract int DesiredWidth();
  77. public abstract void SetColor(Color color);
  78. public IDynamicEditorHost Host { get; set; }
  79. protected List<Button> CreateButtons(out bool bDisableEditor)
  80. {
  81. var buttons = new List<Button>();
  82. bDisableEditor = false;
  83. if (EditorDefinition != null && EditorDefinition is IButtonEditor)
  84. {
  85. var ce = (IButtonEditor)EditorDefinition;
  86. if (ce.Buttons != null)
  87. foreach (var ceb in ce.Buttons.Reverse())
  88. {
  89. if (ceb.DisableEditor)
  90. bDisableEditor = true;
  91. var button = new Button
  92. {
  93. HorizontalAlignment = HorizontalAlignment.Left,
  94. VerticalAlignment = VerticalAlignment.Stretch,
  95. VerticalContentAlignment = VerticalAlignment.Center,
  96. HorizontalContentAlignment = HorizontalAlignment.Center,
  97. Content = ceb.Caption,
  98. Width = ceb.Width,
  99. Margin = new Thickness(5, 0, 0, 0),
  100. Padding = new Thickness(5, 0, 5, 0),
  101. Tag = ceb,
  102. IsEnabled = ceb.IsEnabled
  103. };
  104. button.Click += (o, e) =>
  105. {
  106. var b = (Button)o;
  107. var eb = b.Tag as EditorButton;
  108. eb.Click(this);
  109. };
  110. ceb.OnEnabled += (enabled) =>
  111. {
  112. button.IsEnabled = enabled;
  113. };
  114. buttons.Add(button);
  115. }
  116. }
  117. return buttons;
  118. }
  119. }
  120. public abstract class BaseDynamicEditorControl<TEditor> : BaseDynamicEditorControl
  121. where TEditor : IBaseEditor
  122. {
  123. public new TEditor EditorDefinition
  124. {
  125. get => (TEditor)base.EditorDefinition;
  126. set
  127. {
  128. base.EditorDefinition = value;
  129. }
  130. }
  131. }
  132. }