DFStringControl.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Syncfusion.Windows.Shared;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.Globalization;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. namespace InABox.DynamicGrid
  14. {
  15. public class DFStringControl : DynamicFormFieldControl<DFLayoutStringField, DFLayoutStringFieldProperties, string, string?>
  16. {
  17. private string? text;
  18. private TextBox? TextBox;
  19. private Button? Btn;
  20. [NotNull]
  21. private string? Text
  22. {
  23. get => (Field.Properties.PopupEditor ? text : TextBox?.Text) ?? "";
  24. set
  25. {
  26. if (Field.Properties.PopupEditor)
  27. {
  28. text = value;
  29. }
  30. else if(TextBox is not null)
  31. {
  32. TextBox.Text = value;
  33. }
  34. }
  35. }
  36. public override void SetSerializedValue(string? value)
  37. {
  38. Text = value;
  39. }
  40. public override string? GetSerializedValue()
  41. {
  42. return Text;
  43. }
  44. protected override FrameworkElement Create()
  45. {
  46. FrameworkElement element;
  47. if (Field.Properties.PopupEditor)
  48. {
  49. Btn = new Button { Content = "..." };
  50. Btn.Click += Btn_Click;
  51. element = Btn;
  52. }
  53. else
  54. {
  55. TextBox = new TextBox();
  56. TextBox.Text = Field.Properties.Default;
  57. TextBox.TextWrapping = Field.Properties.TextWrapping ? TextWrapping.Wrap : TextWrapping.NoWrap;
  58. TextBox.VerticalContentAlignment = VerticalAlignment.Top;
  59. TextBox.TextAlignment = TextAlignment.Left;
  60. TextBox.TextChanged += (sender, e) => ChangeField();
  61. element = TextBox;
  62. }
  63. return element;
  64. }
  65. private void Btn_Click(object sender, RoutedEventArgs e)
  66. {
  67. var text = Text;
  68. if(TextBoxDialog.Execute("Edit Comment", ref text))
  69. {
  70. Text = text;
  71. }
  72. }
  73. public override string GetValue() => Text;
  74. public override void SetValue(string? value) => Text = value;
  75. protected override bool IsEmpty() => string.IsNullOrWhiteSpace(Text);
  76. }
  77. }