DynamicGridCodeColumn.cs 2.9 KB

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