TextBoxEditorControl.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. dock.Width += button.Width + 5;
  27. }
  28. Editor = new TextBox
  29. {
  30. VerticalAlignment = VerticalAlignment.Stretch,
  31. VerticalContentAlignment = VerticalAlignment.Center,
  32. HorizontalAlignment = HorizontalAlignment.Stretch
  33. };
  34. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  35. if (DisableEditor)
  36. {
  37. Editor.Background = new SolidColorBrush(Colors.Silver);
  38. Editor.IsEnabled = false;
  39. }
  40. Editor.TextChanged += (o, e) =>
  41. {
  42. if(Loaded)
  43. IsChanged = true;
  44. //CheckChanged();
  45. };
  46. Editor.LostFocus += (o, e) =>
  47. {
  48. if (IsChanged)
  49. CheckChanged();
  50. };
  51. dock.Children.Add(Editor);
  52. return dock;
  53. }
  54. public override int DesiredHeight()
  55. {
  56. return 25;
  57. }
  58. public override int DesiredWidth()
  59. {
  60. return int.MaxValue;
  61. }
  62. protected override string RetrieveValue()
  63. {
  64. return Editor.Text;
  65. }
  66. protected override void UpdateValue(string value)
  67. {
  68. if (Editor != null && value != Editor.Text)
  69. {
  70. Editor.Text = value;
  71. if (Loaded)
  72. CheckChanged();
  73. }
  74. }
  75. public override void SetFocus()
  76. {
  77. Editor.Focus();
  78. Editor.CaretIndex = string.IsNullOrEmpty(Value) ? 0 : Value.Length;
  79. }
  80. public override void SetColor(Color color)
  81. {
  82. Editor.Background = new SolidColorBrush(color);
  83. }
  84. }