DynamicGridCodeColumn.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Data;
  4. using System.Windows.Media;
  5. using InABox.Core;
  6. using InABox.WPF;
  7. using Syncfusion.UI.Xaml.Grid;
  8. using Syncfusion.UI.Xaml.TreeGrid;
  9. namespace InABox.DynamicGrid;
  10. public class DynamicGridCodeColumn : DynamicGridEditorColumn<CodeEditor, GridTemplateColumn, TreeGridTemplateColumn>
  11. {
  12. protected override void Configure(GridTemplateColumn column, CodeEditor editor)
  13. {
  14. column.CellTemplate = CreateCellTemplate(editor, MappingName);
  15. column.EditTemplate = CreateEditTemplate(editor, column, MappingName);
  16. }
  17. protected override void Configure(TreeGridTemplateColumn column, CodeEditor editor)
  18. {
  19. column.CellTemplate = CreateCellTemplate(editor, TreeMappingName);
  20. column.EditTemplate = CreateEditTemplate(editor, column, TreeMappingName);
  21. }
  22. private DataTemplate CreateCellTemplate(CodeEditor editor, string mapping)
  23. {
  24. return TemplateGenerator.CreateDataTemplate
  25. (
  26. () =>
  27. {
  28. var result = new Label();
  29. result.HorizontalContentAlignment = GetHorizontalAlignment(editor);
  30. result.VerticalContentAlignment = GetVerticalAlignment(editor);
  31. var binding = new Binding()
  32. {
  33. Path = new PropertyPath(mapping),
  34. };
  35. result.SetBinding(Label.ContentProperty, binding);
  36. return result;
  37. }
  38. );
  39. }
  40. private DataTemplate CreateEditTemplate(CodeEditor editor, GridColumnBase column, string mapping)
  41. {
  42. return TemplateGenerator.CreateDataTemplate
  43. (
  44. () =>
  45. {
  46. var textbox = new TextBox();
  47. textbox.CharacterCasing = CharacterCasing.Upper;
  48. textbox.TextAlignment = column.TextAlignment;
  49. textbox.SetBinding(TextBox.TextProperty, new Binding()
  50. {
  51. Path = new PropertyPath(mapping)
  52. });
  53. textbox.SetValue(Grid.ColumnSpanProperty, 2);
  54. textbox.Padding = new Thickness(2, 0, 0, 0);
  55. textbox.VerticalContentAlignment = VerticalAlignment.Center;
  56. textbox.PreviewTextInput += (sender, args) => textbox.Tag = true;
  57. textbox.TextChanged += (sender, args) =>
  58. {
  59. if (Equals(textbox.Tag, false))
  60. {
  61. textbox.SelectAll();
  62. textbox.Tag = true;
  63. }
  64. };
  65. textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
  66. return textbox;
  67. }
  68. );
  69. }
  70. public DynamicGridCodeColumn(DynamicGridColumn definition) : base(definition)
  71. {
  72. }
  73. }