DFStringControl.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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>
  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. protected override FrameworkElement Create()
  37. {
  38. FrameworkElement element;
  39. if (Field.Properties.PopupEditor)
  40. {
  41. Btn = new Button { Content = "..." };
  42. Btn.Click += Btn_Click;
  43. element = Btn;
  44. }
  45. else
  46. {
  47. TextBox = new TextBox();
  48. TextBox.Text = Field.Properties.Default;
  49. TextBox.TextWrapping = Field.Properties.TextWrapping ? TextWrapping.Wrap : TextWrapping.NoWrap;
  50. TextBox.VerticalContentAlignment = VerticalAlignment.Top;
  51. TextBox.TextAlignment = TextAlignment.Left;
  52. TextBox.TextChanged += (sender, e) => ChangeField();
  53. element = TextBox;
  54. }
  55. return element;
  56. }
  57. private void Btn_Click(object sender, RoutedEventArgs e)
  58. {
  59. var text = Text;
  60. if(TextBoxDialog.Execute("Edit Comment", ref text))
  61. {
  62. Text = text;
  63. }
  64. }
  65. public override string GetValue() => Text;
  66. public override void SetValue(string? value) => Text = value;
  67. protected override bool IsEmpty() => string.IsNullOrWhiteSpace(Text);
  68. }
  69. }