DynamicEnclosedEditorControl.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media;
  6. using InABox.Core;
  7. namespace InABox.DynamicGrid
  8. {
  9. public abstract class DynamicEnclosedEditorControl<T, TEditor> : BaseDynamicEditorControl<TEditor>
  10. where T : IEnclosedEntity
  11. where TEditor : BaseEditor
  12. {
  13. protected bool Updating;
  14. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  15. public override bool Changed { get; set; }
  16. public override event EditorControlValueChangedHandler? OnEditorValueChanged;
  17. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  18. protected virtual Dictionary<string, object?> OtherValues { get; }
  19. public DynamicEnclosedEditorControl()
  20. {
  21. Loaded = false;
  22. OtherValues = new();
  23. MinHeight = 25;
  24. Focusable = false;
  25. }
  26. protected virtual bool CheckChanged()
  27. {
  28. //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
  29. if (Loaded && !Updating)
  30. {
  31. Updating = true;
  32. try
  33. {
  34. var values = new Dictionary<string, object?>();
  35. var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
  36. foreach(var (k, v) in GetValues())
  37. {
  38. values[k] = v;
  39. }
  40. foreach (var key in OtherValues.Keys)
  41. values[key] = OtherValues[key];
  42. OnEditorValueChanged?.Invoke(this, values);
  43. }
  44. finally
  45. {
  46. Changed = true;
  47. Updating = false;
  48. }
  49. }
  50. return Changed;
  51. }
  52. public override void SetEnabled(bool enabled)
  53. {
  54. if(Content is FrameworkElement element) element.IsEnabled = enabled;
  55. SetColor(enabled ? Color : Colors.WhiteSmoke);
  56. }
  57. public override void SetVisible(bool visible)
  58. {
  59. Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  60. }
  61. protected abstract IEnumerable<KeyValuePair<string, object?>> GetChildValues();
  62. public override Dictionary<string, object?> GetValues()
  63. {
  64. return GetChildValues()
  65. .Select(x => new KeyValuePair<string, object?>($"{ColumnName}.{x.Key}", x.Value))
  66. .ToDictionary();
  67. }
  68. protected abstract object? GetChildValue(string property);
  69. public override object? GetValue(string property)
  70. {
  71. if (!property.StartsWith($"{ColumnName}.")) return null;
  72. property = property[(ColumnName.Length + 1)..];
  73. return GetChildValue(property);
  74. }
  75. protected abstract void SetChildValue(string property, object? value);
  76. public override void SetValue(string property, object? value)
  77. {
  78. if (!property.StartsWith($"{ColumnName}.")) return;
  79. property = property[(ColumnName.Length + 1)..];
  80. SetChildValue(property, value);
  81. base.SetValue(property,value);
  82. }
  83. }
  84. }