DynamicGridUniqueCodeColumn.cs 3.0 KB

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