TextBoxEditorControl.cs 2.4 KB

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