DynamicFormFieldControl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. public void SetEntityValue(object? value);
  25. /// <summary>
  26. /// Gets additional data for this field by the specific field name of <paramref name="field"/>.
  27. /// </summary>
  28. /// <param name="field">A name which specifies what data is requested.</param>
  29. /// <returns>The additional data.</returns>
  30. public object? GetData(string field);
  31. /// <summary>
  32. /// Gets additional data for this field that should be saved into the form data.
  33. /// </summary>
  34. /// <returns>The additional data.</returns>
  35. public Dictionary<string, object?>? GetAdditionalValues();
  36. /// <summary>
  37. /// Check that the data is valid - if it is not, output a message for the user.
  38. /// This function gets called when the user completes a form, or edits an already completed form.
  39. /// </summary>
  40. /// <param name="message">The message to the user.</param>
  41. /// <returns><see langword="true"/> if the data is valid.</returns>
  42. public bool Validate([NotNullWhen(false)] out string? message);
  43. }
  44. public abstract class DynamicFormFieldControl<TField, TProperties, TValue> : DynamicFormControl<TField>, IDynamicFormFieldControl
  45. where TField : DFLayoutField<TProperties>
  46. where TProperties : DFLayoutFieldProperties<TValue>, new()
  47. {
  48. public event FieldChangedEvent? FieldChangedEvent;
  49. public TField Field { get => Control; set => Control = value; }
  50. protected void ChangeField() => FieldChangedEvent?.Invoke();
  51. /// <summary>
  52. /// Checks whether the user has supplied a field - for use with <see cref="Validate(out string?)"/>.
  53. /// </summary>
  54. /// <returns><see langword="true"/> if the user has not supplied a value.</returns>
  55. protected abstract bool IsEmpty();
  56. public virtual bool Validate([NotNullWhen(false)] out string? message)
  57. {
  58. if(Field.Properties.Required && IsEmpty())
  59. {
  60. message = $"Field [{Field.Name}] is required!";
  61. return false;
  62. }
  63. message = null;
  64. return true;
  65. }
  66. protected override void AfterSetControl(TField control)
  67. {
  68. base.AfterSetControl(control);
  69. if (!string.IsNullOrWhiteSpace(control.Properties.Expression))
  70. {
  71. IsEnabled = false;
  72. }
  73. }
  74. public abstract TValue GetValue();
  75. public abstract void SetValue(TValue? value);
  76. public virtual object? GetEntityValue() => GetValue();
  77. public virtual void SetEntityValue(object? value) => SetValue(CoreUtils.ChangeType<TValue>(value));
  78. public virtual object? GetData(string dataField)
  79. {
  80. return null;
  81. }
  82. public virtual Dictionary<string, object?>? GetAdditionalValues()
  83. {
  84. return null;
  85. }
  86. object? IDynamicFormFieldControl.GetValue() => GetValue();
  87. void IDynamicFormFieldControl.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
  88. }
  89. }