DFSignatureControl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media.Imaging;
  11. namespace InABox.DynamicGrid
  12. {
  13. public class DFSignatureControl : DynamicFormFieldControl<DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[]>
  14. {
  15. private Grid Grid = null!; // Late-initialised
  16. private Image Image = null!; // Late-initialised
  17. private bool _isEmpty = true;
  18. protected override FrameworkElement Create()
  19. {
  20. Grid = new Grid();
  21. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  22. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  23. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
  24. Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
  25. Grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
  26. Image = new Image();
  27. Image.StretchDirection = StretchDirection.DownOnly;
  28. Image.Source = EmbeddedImageUtilities.CreateEmptySignature();
  29. var clearButton = new Button
  30. {
  31. Content = "Clear",
  32. Margin = new Thickness(0, 5, 5, 0),
  33. Width = 60,
  34. Height = 35
  35. };
  36. clearButton.Tag = Image;
  37. clearButton.Click += SignatureClear_Click;
  38. var editButton = new Button
  39. {
  40. Content = "Edit",
  41. Margin = new Thickness(0, 5, 0, 0),
  42. Width = 60,
  43. Height = 35
  44. };
  45. editButton.Tag = Image;
  46. editButton.Click += SignatureEdit_Click;
  47. Image.SetGridPosition(0, 0, 1, 3);
  48. clearButton.SetGridPosition(1, 1, 1, 1);
  49. editButton.SetGridPosition(1, 2, 1, 1);
  50. Grid.Children.Add(Image);
  51. Grid.Children.Add(clearButton);
  52. Grid.Children.Add(editButton);
  53. return Grid;
  54. }
  55. private void SignatureClear_Click(object sender, RoutedEventArgs e)
  56. {
  57. if (!_isEmpty)
  58. {
  59. Image.Source = EmbeddedImageUtilities.CreateEmptySignature();
  60. _isEmpty = true;
  61. ChangeField();
  62. }
  63. }
  64. private void SignatureEdit_Click(object sender, RoutedEventArgs e)
  65. {
  66. var window = new SignaturePadWindow(_isEmpty ? null : Image.Source);
  67. if (window.ShowDialog() == true)
  68. {
  69. Image.Source = window.Image ?? EmbeddedImageUtilities.CreateEmptySignature();
  70. _isEmpty = window.Image == null;
  71. ChangeField();
  72. }
  73. }
  74. public override byte[] GetValue()
  75. {
  76. return EmbeddedImageUtilities.SaveImageToBytes(Image, _isEmpty, new PngBitmapEncoder()) ?? Array.Empty<byte>();
  77. }
  78. public override void SetValue(byte[]? value)
  79. {
  80. _isEmpty = !EmbeddedImageUtilities.LoadImageFromData(Image, value, () => EmbeddedImageUtilities.CreateEmptySignature());
  81. }
  82. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  83. {
  84. base.OnPropertyChanged(e);
  85. if (e.Property == IsEnabledProperty)
  86. {
  87. Grid.RowDefinitions[1].Height = (bool)e.NewValue
  88. ? GridLength.Auto
  89. : new GridLength(0);
  90. }
  91. }
  92. protected override bool IsEmpty() => _isEmpty;
  93. }
  94. }