DynamicFormFieldControl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using InABox.Core;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using Xceed.Wpf.Toolkit.PropertyGrid.Converters;
  10. namespace InABox.DynamicGrid;
  11. public delegate void FieldChangedEvent();
  12. public interface IDynamicFormFieldControl
  13. {
  14. public event FieldChangedEvent? FieldChangedEvent;
  15. void Deserialize(DFLoadStorageEntry storage);
  16. void Serialize(DFSaveStorageEntry storage);
  17. public object? GetValue();
  18. /// <summary>
  19. /// Sets the value in this control.
  20. /// </summary>
  21. /// <param name="value">The value to set. This will be the return value from a call to <see cref="DFLayoutFieldProperties.ParseValue(object)"/>.</param>
  22. public void SetValue(object? value);
  23. public object? GetEntityValue();
  24. /// <summary>
  25. /// Gets additional data for this field by the specific field name of <paramref name="field"/>.
  26. /// </summary>
  27. /// <param name="field">A name which specifies what data is requested.</param>
  28. /// <returns>The additional data.</returns>
  29. public object? GetData(string field);
  30. /// <summary>
  31. /// Check that the data is valid - if it is not, output a message for the user.
  32. /// This function gets called when the user completes a form, or edits an already completed form.
  33. /// </summary>
  34. /// <param name="message">The message to the user.</param>
  35. /// <returns><see langword="true"/> if the data is valid.</returns>
  36. public bool Validate([NotNullWhen(false)] out string? message);
  37. }
  38. public abstract class DynamicFormFieldControl<TField, TProperties, TValue, TSerialized> : DynamicFormControl<TField>, IDynamicFormFieldControl
  39. where TField : DFLayoutField<TProperties>
  40. where TProperties : DFLayoutFieldProperties<TValue, TSerialized>, new()
  41. {
  42. public event FieldChangedEvent? FieldChangedEvent;
  43. public TField Field { get => Control; set => Control = value; }
  44. public void Serialize(DFSaveStorageEntry storage)
  45. {
  46. Field.Properties.SerializeValue(storage, GetSerializedValue());
  47. }
  48. public void Deserialize(DFLoadStorageEntry storage)
  49. {
  50. SetSerializedValue(Field.Properties.DeserializeValue(storage));
  51. }
  52. protected void ChangeField() => FieldChangedEvent?.Invoke();
  53. /// <summary>
  54. /// Checks whether the user has supplied a field - for use with <see cref="Validate(out string?)"/>.
  55. /// </summary>
  56. /// <returns><see langword="true"/> if the user has not supplied a value.</returns>
  57. protected abstract bool IsEmpty();
  58. public virtual bool Validate([NotNullWhen(false)] out string? message)
  59. {
  60. if(Field.Properties.Required && IsEmpty())
  61. {
  62. message = $"Field [{Field.Name}] is required!";
  63. return false;
  64. }
  65. message = null;
  66. return true;
  67. }
  68. protected override void AfterSetControl(TField control)
  69. {
  70. base.AfterSetControl(control);
  71. if (!string.IsNullOrWhiteSpace(control.Properties.Expression)
  72. || (!control.Properties.Property.IsNullOrWhiteSpace() && control.Properties.ReadOnlyProperty))
  73. {
  74. IsEnabled = false;
  75. }
  76. }
  77. public abstract TSerialized GetSerializedValue();
  78. public abstract void SetSerializedValue(TSerialized value);
  79. public abstract TValue GetValue();
  80. public abstract void SetValue(TValue? value);
  81. public virtual object? GetEntityValue() => GetValue();
  82. public virtual object? GetData(string dataField)
  83. {
  84. return null;
  85. }
  86. object? IDynamicFormFieldControl.GetValue() => GetValue();
  87. void IDynamicFormFieldControl.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
  88. }