DFStringControl.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.HorizontalContentAlignment = HorizontalAlignment.Left;
  50. TextBox.VerticalContentAlignment = VerticalAlignment.Center;
  51. TextBox.TextChanged += (sender, e) => ChangeField();
  52. element = TextBox;
  53. }
  54. return element;
  55. }
  56. private void Btn_Click(object sender, RoutedEventArgs e)
  57. {
  58. var text = Text;
  59. if(TextBoxDialog.Execute("Edit Comment", ref text))
  60. {
  61. Text = text;
  62. }
  63. }
  64. public override string GetValue() => Text;
  65. public override void SetValue(string? value) => Text = value;
  66. protected override bool IsEmpty() => string.IsNullOrWhiteSpace(Text);
  67. }
  68. }