DFStringFieldControl.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. using CommunityToolkit.Mvvm.Input;
  7. using DialogHostAvalonia;
  8. using InABox.Avalonia;
  9. using InABox.Avalonia.Components;
  10. using InABox.Core;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics.CodeAnalysis;
  14. using System.Globalization;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using InABox.Avalonia.Dialogs;
  19. namespace PRS.Avalonia.DigitalForms;
  20. partial class DFStringFieldControl : DigitalFormFieldControl<DFLayoutStringField, DFLayoutStringFieldProperties, string, string?>
  21. {
  22. private string? text;
  23. private TextBox? TextBox;
  24. private Button? Btn;
  25. private TextBlock? TextBlock;
  26. private bool _initialised;
  27. [NotNull]
  28. private string? Text
  29. {
  30. get => (Field.Properties.PopupEditor ? text : TextBox?.Text) ?? "";
  31. set
  32. {
  33. if (Field.Properties.PopupEditor)
  34. {
  35. text = value;
  36. TextBlock!.Text = value;
  37. }
  38. else if(TextBox is not null)
  39. {
  40. TextBox.Text = value;
  41. }
  42. }
  43. }
  44. public override void SetSerializedValue(string? value)
  45. {
  46. Text = value;
  47. }
  48. public override string? GetSerializedValue()
  49. {
  50. return Text;
  51. }
  52. protected override void OnLoaded(RoutedEventArgs e)
  53. {
  54. base.OnLoaded(e);
  55. _initialised = true;
  56. }
  57. protected override Control Create()
  58. {
  59. if (Field.Properties.PopupEditor)
  60. {
  61. var panel = new DockPanel();
  62. Btn = new Button { Content = "..." };
  63. Btn.Command = PopupCommand;
  64. TextBlock = new TextBlock();
  65. TextBlock.Classes.Add("DFStringFieldControl");
  66. DockPanel.SetDock(Btn, Dock.Left);
  67. DockPanel.SetDock(TextBlock, Dock.Right);
  68. panel.Children.Add(Btn);
  69. panel.Children.Add(TextBlock);
  70. return panel;
  71. }
  72. else
  73. {
  74. TextBox = new TextBox();
  75. TextBox.Text = Field.Properties.Default;
  76. TextBox.TextWrapping = Field.Properties.TextWrapping ? TextWrapping.Wrap : TextWrapping.NoWrap;
  77. TextBox.TextAlignment = TextAlignment.Left;
  78. TextBox.VerticalContentAlignment = VerticalAlignment.Center;
  79. TextBox.VerticalAlignment = VerticalAlignment.Stretch;
  80. TextBox.TextChanged += (sender, e) =>
  81. {
  82. if (!_initialised) return;
  83. ChangeField();
  84. };
  85. return TextBox;
  86. }
  87. }
  88. public override void SetBackground(IBrush brush, bool defaultColour)
  89. {
  90. if (Field.Properties.PopupEditor)
  91. {
  92. Background = brush;
  93. }
  94. else
  95. {
  96. TextBox!.Background = brush;
  97. }
  98. }
  99. [RelayCommand]
  100. private async Task Popup()
  101. {
  102. var result = await Navigation.Popup<TextDialogViewModel, string?>(x =>
  103. {
  104. x.Text = Text;
  105. x.Multiline = true;
  106. });
  107. if(result is not null)
  108. {
  109. Text = result;
  110. ChangeField();
  111. }
  112. }
  113. public override string GetValue() => Text;
  114. public override void SetValue(string? value) => Text = value;
  115. protected override bool IsEmpty() => string.IsNullOrWhiteSpace(Text);
  116. public override void SetEnabled(bool enabled)
  117. {
  118. IsEnabled = enabled;
  119. }
  120. }