DynamicGridMaskColumn.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using InABox.WPF;
  10. using Microsoft.Xaml.Behaviors;
  11. using Syncfusion.UI.Xaml.Grid;
  12. namespace InABox.DynamicGrid;
  13. public abstract class DynamicGridMaskColumn<TEntity, TEditor> : DynamicGridEditorColumn<TEntity, TEditor, GridTemplateColumn>
  14. where TEntity : BaseObject
  15. where TEditor : class,new()
  16. {
  17. protected abstract Behavior CreateBehaviour();
  18. protected abstract IValueConverter CreateConverter();
  19. protected abstract Button[] CreateButtons(TextBox textbox);
  20. protected override void Configure(GridTemplateColumn column, TEditor editor)
  21. {
  22. var converter = CreateConverter();
  23. column.CellTemplate = TemplateGenerator.CreateDataTemplate
  24. (
  25. () =>
  26. {
  27. var result = new Label();
  28. result.HorizontalContentAlignment = Column.TextAlignment == TextAlignment.Left
  29. ? HorizontalAlignment.Left
  30. : Column.TextAlignment == TextAlignment.Center
  31. ? HorizontalAlignment.Center
  32. : HorizontalAlignment.Right;
  33. var binding = new Binding()
  34. {
  35. Path = new PropertyPath(MappingName),
  36. Converter = converter
  37. };
  38. result.SetBinding(Label.ContentProperty, binding);
  39. return result;
  40. }
  41. );
  42. column.EditTemplate = TemplateGenerator.CreateDataTemplate
  43. (
  44. () =>
  45. {
  46. var result = new Grid();
  47. result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star)});
  48. result.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto)});
  49. var textbox = new TextBox();
  50. textbox.CharacterCasing = CharacterCasing.Upper;
  51. textbox.TextAlignment = Column.TextAlignment;
  52. textbox.SetBinding(TextBox.TextProperty, new Binding()
  53. {
  54. Path = new PropertyPath(MappingName),
  55. Converter = converter
  56. });
  57. textbox.SetValue(Grid.ColumnSpanProperty, 2);
  58. textbox.Padding = new Thickness(2, 0, 0, 0);
  59. textbox.VerticalContentAlignment = VerticalAlignment.Center;
  60. textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
  61. Interaction.GetBehaviors(textbox).Add(CreateBehaviour());
  62. result.Children.Add(textbox);
  63. var padding = 0.0;
  64. var buttons = CreateButtons(textbox);
  65. if (buttons?.Any() == true)
  66. {
  67. StackPanel stack = new StackPanel()
  68. {
  69. Orientation = Orientation.Horizontal
  70. };
  71. stack.SetValue(Grid.ColumnProperty,1);
  72. result.Children.Add(stack);
  73. foreach (var button in buttons)
  74. {
  75. button.Margin = new Thickness(1);
  76. button.BorderThickness = new Thickness(0.75, 0, 0, 0);
  77. stack.Children.Add(button);
  78. padding += button.Width;
  79. }
  80. }
  81. textbox.Padding = new Thickness(0, 0, padding, 0);
  82. return result;
  83. }
  84. );
  85. column.SetCellBoundValue = false;
  86. }
  87. protected DynamicGridMaskColumn(DynamicGridColumn definition) : base(definition)
  88. {
  89. }
  90. }