123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using InABox.Core;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using Xceed.Wpf.Toolkit.PropertyGrid.Converters;
- namespace InABox.DynamicGrid
- {
- public delegate void FieldChangedEvent();
- public interface IDynamicFormFieldControl
- {
- public event FieldChangedEvent? FieldChangedEvent;
-
- void Deserialize(DFLoadStorageEntry storage);
- void Serialize(DFSaveStorageEntry storage);
- public object? GetValue();
- /// <summary>
- /// Sets the value in this control.
- /// </summary>
- /// <param name="value">The value to set. This will be the return value from a call to <see cref="DFLayoutFieldProperties.ParseValue(object)"/>.</param>
- public void SetValue(object? value);
- public object? GetEntityValue();
- public void SetEntityValue(object? value);
- /// <summary>
- /// Gets additional data for this field by the specific field name of <paramref name="field"/>.
- /// </summary>
- /// <param name="field">A name which specifies what data is requested.</param>
- /// <returns>The additional data.</returns>
- public object? GetData(string field);
- /// <summary>
- /// Gets additional data for this field that should be saved into the form data.
- /// </summary>
- /// <returns>The additional data.</returns>
- public Dictionary<string, object?>? GetAdditionalValues();
- /// <summary>
- /// Check that the data is valid - if it is not, output a message for the user.
- /// This function gets called when the user completes a form, or edits an already completed form.
- /// </summary>
- /// <param name="message">The message to the user.</param>
- /// <returns><see langword="true"/> if the data is valid.</returns>
- public bool Validate([NotNullWhen(false)] out string? message);
- }
- public abstract class DynamicFormFieldControl<TField, TProperties, TValue, TSerialized> : DynamicFormControl<TField>, IDynamicFormFieldControl
- where TField : DFLayoutField<TProperties>
- where TProperties : DFLayoutFieldProperties<TValue, TSerialized>, new()
- {
- public event FieldChangedEvent? FieldChangedEvent;
- public TField Field { get => Control; set => Control = value; }
- public void Serialize(DFSaveStorageEntry storage)
- {
- Field.Properties.SerializeValue(storage, GetSerializedValue());
- }
- public void Deserialize(DFLoadStorageEntry storage)
- {
- SetSerializedValue(Field.Properties.DeserializeValue(storage));
- }
- protected void ChangeField() => FieldChangedEvent?.Invoke();
- /// <summary>
- /// Checks whether the user has supplied a field - for use with <see cref="Validate(out string?)"/>.
- /// </summary>
- /// <returns><see langword="true"/> if the user has not supplied a value.</returns>
- protected abstract bool IsEmpty();
- public virtual bool Validate([NotNullWhen(false)] out string? message)
- {
- if(Field.Properties.Required && IsEmpty())
- {
- message = $"Field [{Field.Name}] is required!";
- return false;
- }
- message = null;
- return true;
- }
- protected override void AfterSetControl(TField control)
- {
- base.AfterSetControl(control);
- if (!string.IsNullOrWhiteSpace(control.Properties.Expression) || control.Properties.ReadOnlyProperty)
- {
- IsEnabled = false;
- }
- }
- public abstract TSerialized GetSerializedValue();
- public abstract void SetSerializedValue(TSerialized value);
- public abstract TValue GetValue();
- public abstract void SetValue(TValue? value);
- public virtual object? GetEntityValue() => GetValue();
- public virtual void SetEntityValue(object? value) => SetValue(CoreUtils.ChangeType<TValue>(value));
- public virtual object? GetData(string dataField)
- {
- return null;
- }
-
- public virtual Dictionary<string, object?>? GetAdditionalValues()
- {
- return null;
- }
- object? IDynamicFormFieldControl.GetValue() => GetValue();
- void IDynamicFormFieldControl.SetValue(object? value) => SetValue(value != null ? (TValue)value : default);
- }
- }
|