DynamicEnclosedEditorControl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /// <summary>
  27. /// Trigger a change notification.
  28. /// </summary>
  29. /// <param name="changedFields">A list of fields that have been changed. If empty, then it is assumed that all child fields are changed.</param>
  30. protected virtual bool CheckChanged(params string[] changedFields)
  31. {
  32. //Logger.Send(LogType.Information, "", string.Format("{0}({1}).CheckChanged()", GetType().EntityName().Split('.').Last(), ColumnName));
  33. if (Loaded && !Updating)
  34. {
  35. Updating = true;
  36. try
  37. {
  38. var values = new Dictionary<string, object?>();
  39. var sColumn = string.IsNullOrEmpty(ColumnName) ? "" : ColumnName;
  40. if(changedFields.Length > 0)
  41. {
  42. foreach(var field in changedFields)
  43. {
  44. values[$"{ColumnName}.{field}"] = GetChildValue(field);
  45. }
  46. }
  47. else
  48. {
  49. foreach(var (k, v) in GetChildValues())
  50. {
  51. values[$"{ColumnName}.{k}"] = v;
  52. }
  53. }
  54. foreach (var key in OtherValues.Keys)
  55. values[key] = OtherValues[key];
  56. OnEditorValueChanged?.Invoke(this, values);
  57. }
  58. finally
  59. {
  60. Changed = true;
  61. Updating = false;
  62. }
  63. }
  64. return Changed;
  65. }
  66. public override void SetEnabled(bool enabled)
  67. {
  68. if(Content is FrameworkElement element) element.IsEnabled = enabled;
  69. SetColor(enabled ? Color : Colors.WhiteSmoke);
  70. }
  71. public override void SetVisible(bool visible)
  72. {
  73. Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
  74. }
  75. protected abstract IEnumerable<KeyValuePair<string, object?>> GetChildValues();
  76. public override Dictionary<string, object?> GetValues()
  77. {
  78. return GetChildValues()
  79. .Select(x => new KeyValuePair<string, object?>($"{ColumnName}.{x.Key}", x.Value))
  80. .ToDictionary();
  81. }
  82. protected abstract object? GetChildValue(string property);
  83. public override object? GetValue(string property)
  84. {
  85. if (!property.StartsWith($"{ColumnName}.")) return null;
  86. property = property[(ColumnName.Length + 1)..];
  87. return GetChildValue(property);
  88. }
  89. protected abstract void SetChildValue(string property, object? value);
  90. public override void SetValue(string property, object? value)
  91. {
  92. if (!property.StartsWith($"{ColumnName}.")) return;
  93. property = property[(ColumnName.Length + 1)..];
  94. SetChildValue(property, value);
  95. base.SetValue(property,value);
  96. }
  97. }
  98. }