DynamicFormFieldControl.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /// Check that the data is valid - if it is not, output a message for the user.
  27. /// This function gets called when the user completes a form, or edits an already completed form.
  28. /// </summary>
  29. /// <param name="message">The message to the user.</param>
  30. /// <returns><see langword="true"/> if the data is valid.</returns>
  31. public bool Validate([NotNullWhen(false)] out string? message);
  32. }
  33. public abstract class DynamicFormFieldControl<TField, TProperties, TValue> : DynamicFormControl<TField>, IDynamicFormFieldControl
  34. where TField : DFLayoutField<TProperties>
  35. where TProperties : DFLayoutFieldProperties<TValue>, new()
  36. {
  37. public event FieldChangedEvent? FieldChangedEvent;
  38. public TField Field { get => Control; set => Control = value; }
  39. protected void ChangeField() => FieldChangedEvent?.Invoke();
  40. /// <summary>
  41. /// Checks whether the user has supplied a field - for use with <see cref="Validate(out string?)"/>.
  42. /// </summary>
  43. /// <returns><see langword="true"/> if the user has not supplied a value.</returns>
  44. protected abstract bool IsEmpty();
  45. public virtual bool Validate([NotNullWhen(false)] out string? message)
  46. {
  47. if(Field.Properties.Required && IsEmpty())
  48. {
  49. message = $"Field [{Field.Name}] is required!";
  50. return false;
  51. }
  52. message = null;
  53. return true;
  54. }
  55. public abstract TValue GetValue();
  56. public abstract void SetValue(TValue? value);
  57. public virtual object? GetEntityValue() => GetValue();
  58. public virtual void SetEntityValue(object? value) => SetValue(CoreUtils.ChangeType<TValue>(value));
  59. object? IDynamicFormFieldControl.GetValue() => GetValue();
  60. void IDynamicFormFieldControl.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
  61. }
  62. }