TextBoxEditorControl.cs 2.6 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. {
  8. public class TextBoxEditorControl : DynamicEditorControl<string, TextBoxEditor>
  9. {
  10. static TextBoxEditorControl()
  11. {
  12. //DynamicEditorControlFactory.Register<TextBoxEditorControl, TextBoxEditor>();
  13. }
  14. private TextBox Editor;
  15. private bool IsChanged;
  16. public override void Configure()
  17. {
  18. }
  19. protected override FrameworkElement CreateEditor()
  20. {
  21. var dock = new DockPanel();
  22. var buttons = CreateButtons(out var DisableEditor);
  23. foreach (var button in buttons)
  24. {
  25. button.SetValue(DockPanel.DockProperty, Dock.Right);
  26. dock.Children.Add(button);
  27. dock.Width += button.Width + 5;
  28. }
  29. Editor = new TextBox
  30. {
  31. VerticalAlignment = VerticalAlignment.Stretch,
  32. VerticalContentAlignment = VerticalAlignment.Center,
  33. HorizontalAlignment = HorizontalAlignment.Stretch
  34. };
  35. Editor.SetValue(DockPanel.DockProperty, Dock.Left);
  36. if (DisableEditor)
  37. {
  38. Editor.Background = new SolidColorBrush(Colors.Silver);
  39. Editor.IsEnabled = false;
  40. }
  41. Editor.TextChanged += (o, e) =>
  42. {
  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. }
  85. }