TextBoxEditorControl.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if (EditorDefinition.ImmediateUpdates)
  45. CheckChanged();
  46. }
  47. };
  48. Editor.LostFocus += (o, e) =>
  49. {
  50. if (IsChanged)
  51. CheckChanged();
  52. };
  53. dock.Children.Add(Editor);
  54. return dock;
  55. }
  56. public override int DesiredHeight()
  57. {
  58. return 25;
  59. }
  60. public override int DesiredWidth()
  61. {
  62. return int.MaxValue;
  63. }
  64. protected override string RetrieveValue()
  65. {
  66. return Editor.Text;
  67. }
  68. protected override void UpdateValue(string value)
  69. {
  70. if (Editor != null && value != Editor.Text)
  71. {
  72. Editor.Text = value;
  73. if (Loaded)
  74. CheckChanged();
  75. }
  76. }
  77. public override void SetFocus()
  78. {
  79. Editor.Focus();
  80. Editor.CaretIndex = string.IsNullOrEmpty(Value) ? 0 : Value.Length;
  81. }
  82. public override void SetColor(Color color)
  83. {
  84. Editor.Background = new SolidColorBrush(color);
  85. }
  86. }