DFStringControl.cs 2.4 KB

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