TextEdit.xaml.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Windows;
  2. namespace InABox.Wpf.Editors
  3. {
  4. public partial class TextEdit : ThemableWindow
  5. {
  6. public TextEdit()
  7. {
  8. InitializeComponent();
  9. }
  10. public TextEdit(string caption, string text = "")
  11. {
  12. InitializeComponent();
  13. Title = caption;
  14. TextEditor.Text = text;
  15. }
  16. public string Text
  17. {
  18. get => TextEditor.Text;
  19. set => TextEditor.Text = value;
  20. }
  21. private void OK_Click(object sender, RoutedEventArgs e)
  22. {
  23. DialogResult = true;
  24. Close();
  25. }
  26. private void Cancel_Click(object sender, RoutedEventArgs e)
  27. {
  28. DialogResult = false;
  29. Close();
  30. }
  31. public static bool Execute(string caption, ref string text)
  32. {
  33. var editor = new TextEdit(caption, text);
  34. if (editor.ShowDialog() == true)
  35. {
  36. text = editor.Text;
  37. return true;
  38. }
  39. return false;
  40. }
  41. private void ThemableWindow_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. TextEditor.Focus();
  44. TextEditor.SelectAll();
  45. }
  46. private void TextEditor_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  47. {
  48. switch (e.Key)
  49. {
  50. case System.Windows.Input.Key.Enter:
  51. DialogResult = true;
  52. Close();
  53. break;
  54. case System.Windows.Input.Key.Escape:
  55. DialogResult = false;
  56. Close();
  57. break;
  58. }
  59. }
  60. }
  61. }