DynamicFormFieldControl.cs 4.1 KB

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