DynamicGridCodeColumn.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. namespace InABox.DynamicGrid;
  9. public class DynamicGridCodeColumn<TEntity> : DynamicGridEditorColumn<TEntity,CodeEditor,GridTemplateColumn> where TEntity : BaseObject
  10. {
  11. protected override void Configure(GridTemplateColumn column, CodeEditor editor)
  12. {
  13. column.CellTemplate = TemplateGenerator.CreateDataTemplate
  14. (
  15. () =>
  16. {
  17. var result = new Label();
  18. result.HorizontalContentAlignment = Column.TextAlignment == TextAlignment.Left
  19. ? HorizontalAlignment.Left
  20. : Column.TextAlignment == TextAlignment.Center
  21. ? HorizontalAlignment.Center
  22. : HorizontalAlignment.Right;
  23. var binding = new Binding()
  24. {
  25. Path = new PropertyPath(MappingName),
  26. };
  27. result.SetBinding(Label.ContentProperty, binding);
  28. return result;
  29. }
  30. );
  31. column.EditTemplate = TemplateGenerator.CreateDataTemplate
  32. (
  33. () =>
  34. {
  35. var textbox = new TextBox();
  36. textbox.CharacterCasing = CharacterCasing.Upper;
  37. textbox.TextAlignment = Column.TextAlignment;
  38. textbox.SetBinding(TextBox.TextProperty, new Binding()
  39. {
  40. Path = new PropertyPath(MappingName)
  41. });
  42. textbox.SetValue(Grid.ColumnSpanProperty, 2);
  43. textbox.Padding = new Thickness(2, 0, 0, 0);
  44. textbox.VerticalContentAlignment = VerticalAlignment.Center;
  45. textbox.PreviewTextInput += (sender, args) => textbox.Tag = true;
  46. textbox.TextChanged += (sender, args) =>
  47. {
  48. if (Equals(textbox.Tag, false))
  49. {
  50. textbox.SelectAll();
  51. textbox.Tag = true;
  52. }
  53. };
  54. textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
  55. return textbox;
  56. }
  57. );
  58. }
  59. public DynamicGridCodeColumn(DynamicGridColumn definition) : base(definition)
  60. {
  61. }
  62. }