TextBoxEditorControl.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Linq;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using InABox.Core;
  6. namespace InABox.DynamicGrid;
  7. public class TextBoxEditorControl : DynamicEditorControl<string, TextBoxEditor>
  8. {
  9. static TextBoxEditorControl()
  10. {
  11. //DynamicEditorControlFactory.Register<TextBoxEditorControl, TextBoxEditor>();
  12. }
  13. private TextBox Editor;
  14. private bool IsChanged;
  15. public override void Configure()
  16. {
  17. }
  18. protected override FrameworkElement CreateEditor()
  19. {
  20. var dock = new DockPanel();
  21. var buttons = CreateButtons(out var DisableEditor);
  22. foreach (var button in buttons)
  23. {
  24. button.SetValue(DockPanel.DockProperty, Dock.Right);
  25. dock.Children.Add(button);
  26. }
  27. Editor = new TextBox
  28. {
  29. VerticalAlignment = VerticalAlignment.Stretch,
  30. VerticalContentAlignment = VerticalAlignment.Center,
  31. HorizontalAlignment = HorizontalAlignment.Stretch
  32. };
  33. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  34. if (DisableEditor)
  35. {
  36. Editor.Background = new SolidColorBrush(Colors.Silver);
  37. Editor.IsEnabled = false;
  38. }
  39. Editor.TextChanged += (o, e) =>
  40. {
  41. if (Loaded)
  42. {
  43. IsChanged = true;
  44. CheckChanged();
  45. }
  46. };
  47. Editor.LostFocus += (o, e) =>
  48. {
  49. if (IsChanged)
  50. CheckChanged();
  51. };
  52. dock.Children.Add(Editor);
  53. return dock;
  54. }
  55. public override int DesiredHeight()
  56. {
  57. return 25;
  58. }
  59. public override int DesiredWidth()
  60. {
  61. return int.MaxValue;
  62. }
  63. protected override string RetrieveValue()
  64. {
  65. return Editor.Text;
  66. }
  67. protected override void UpdateValue(string value)
  68. {
  69. if (Editor != null && value != Editor.Text)
  70. {
  71. Editor.Text = value;
  72. if (Loaded)
  73. CheckChanged();
  74. }
  75. }
  76. public override void SetFocus()
  77. {
  78. Editor.Focus();
  79. Editor.CaretIndex = string.IsNullOrEmpty(Value) ? 0 : Value.Length;
  80. }
  81. public override void SetColor(Color color)
  82. {
  83. Editor.Background = new SolidColorBrush(color);
  84. }
  85. }