TextBoxEditorControl.cs 2.6 KB

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