BaseDynamicEditorControl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ce && ce.Buttons is not null)
  84. {
  85. foreach (var ceb in ce.Buttons.Reverse())
  86. {
  87. if (ceb.DisableEditor)
  88. bDisableEditor = true;
  89. var button = new Button
  90. {
  91. HorizontalAlignment = HorizontalAlignment.Left,
  92. VerticalAlignment = VerticalAlignment.Stretch,
  93. VerticalContentAlignment = VerticalAlignment.Center,
  94. HorizontalContentAlignment = HorizontalAlignment.Center,
  95. Content = ceb.Caption,
  96. Width = ceb.Width,
  97. Margin = new Thickness(5, 0, 0, 0),
  98. Padding = new Thickness(5, 0, 5, 0),
  99. Tag = ceb,
  100. IsEnabled = ceb.IsEnabled,
  101. Visibility = ceb.IsVisible ? Visibility.Visible : Visibility.Collapsed
  102. };
  103. button.Click += (o, e) =>
  104. {
  105. var b = (Button)o;
  106. var eb = b.Tag as EditorButton;
  107. eb.Click(this);
  108. };
  109. ceb.OnVisible += (visible) =>
  110. {
  111. button.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  112. };
  113. ceb.OnEnabled += (enabled) =>
  114. {
  115. button.IsEnabled = enabled;
  116. };
  117. buttons.Add(button);
  118. }
  119. }
  120. return buttons;
  121. }
  122. }
  123. public abstract class BaseDynamicEditorControl<TEditor> : BaseDynamicEditorControl
  124. where TEditor : IBaseEditor
  125. {
  126. public new TEditor EditorDefinition
  127. {
  128. get => (TEditor)base.EditorDefinition;
  129. set
  130. {
  131. base.EditorDefinition = value;
  132. }
  133. }
  134. }
  135. }