TextBoxEditorControl.cs 2.5 KB

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