DynamicGridMaskColumn.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using InABox.Core;
  11. using InABox.WPF;
  12. using Microsoft.Xaml.Behaviors;
  13. using Syncfusion.UI.Xaml.Grid;
  14. using Syncfusion.UI.Xaml.TreeGrid;
  15. namespace InABox.DynamicGrid;
  16. public class DynamicGridMaskColumnButtonClickArgs
  17. {
  18. public object? Value { get; set; }
  19. public DynamicGridMaskColumnButtonClickArgs(object? value)
  20. {
  21. Value = value;
  22. }
  23. }
  24. public enum DynamicGridMaskColumnButtonPosition
  25. {
  26. Left,
  27. Right
  28. }
  29. public class DynamicGridMaskColumnButton
  30. {
  31. public BitmapImage? Image { get; set; }
  32. public Visibility Visibility { get; set; }
  33. public DynamicGridMaskColumnButtonPosition Position { get; set; } = DynamicGridMaskColumnButtonPosition.Right;
  34. public Action<DynamicGridMaskColumnButton,DynamicGridMaskColumnButtonClickArgs>? Clicked { get; set; }
  35. public object? DoClick(object? value)
  36. {
  37. var args = new DynamicGridMaskColumnButtonClickArgs(value);
  38. Clicked?.Invoke(this, args);
  39. return args.Value;
  40. }
  41. }
  42. public abstract class DynamicGridMaskColumn<TEntity, TEditor> : DynamicGridEditorColumn<TEntity, TEditor, GridTemplateColumn, TreeGridTemplateColumn>
  43. where TEntity : BaseObject
  44. where TEditor : class,new()
  45. {
  46. protected abstract Behavior? CreateBehaviour();
  47. protected abstract IValueConverter? CreateConverter();
  48. protected virtual Tuple<DataTemplate, DataTemplate> CreateTemplates(GridColumnBase column, TEditor editor, string mapping)
  49. {
  50. var converter = CreateConverter();
  51. var cellTemplate = TemplateGenerator.CreateDataTemplate
  52. (
  53. () =>
  54. {
  55. var result = new Label
  56. {
  57. HorizontalContentAlignment = column.TextAlignment == TextAlignment.Left
  58. ? HorizontalAlignment.Left
  59. : column.TextAlignment == TextAlignment.Center
  60. ? HorizontalAlignment.Center
  61. : HorizontalAlignment.Right,
  62. VerticalContentAlignment = VerticalAlignment.Center
  63. };
  64. var binding = new Binding()
  65. {
  66. Path = new PropertyPath(mapping),
  67. Converter = converter
  68. };
  69. result.SetBinding(Label.ContentProperty, binding);
  70. return result;
  71. }
  72. );
  73. var editTemplate = TemplateGenerator.CreateDataTemplate
  74. (
  75. () =>
  76. {
  77. var border = new Border()
  78. {
  79. BorderBrush = new SolidColorBrush(Colors.Gray),
  80. BorderThickness = new Thickness(0.75),
  81. Padding = new Thickness(0),
  82. Margin = new Thickness(0)
  83. };
  84. var dock = new DockPanel();
  85. dock.SourceUpdated += OnSourceUpdated;
  86. dock.TargetUpdated += OnTargetUpdated;
  87. border.Child = dock;
  88. var textbox = new TextBox
  89. {
  90. CharacterCasing = CharacterCasing.Upper,
  91. TextAlignment = column.TextAlignment,
  92. BorderThickness = new Thickness(0),
  93. //Padding = new Thickness(2, 0, 0, 0),
  94. VerticalContentAlignment = VerticalAlignment.Center,
  95. Background = new SolidColorBrush(Colors.White)
  96. };
  97. textbox.SetBinding(TextBox.TextProperty, new Binding()
  98. {
  99. Path = new PropertyPath(mapping),
  100. Converter = converter,
  101. NotifyOnSourceUpdated = true,
  102. NotifyOnTargetUpdated = true,
  103. });
  104. var behaviour = CreateBehaviour();
  105. if (behaviour != null)
  106. Interaction.GetBehaviors(textbox).Add(behaviour);
  107. textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
  108. textbox.SetValue(DockPanel.DockProperty,Dock.Left);
  109. var defs = CreateButtons();
  110. if (defs?.Any() == true)
  111. {
  112. foreach (var def in defs)
  113. {
  114. Button button = new Button()
  115. {
  116. Tag = def,
  117. Content = new Image() { Source = def.Image },
  118. Padding = new Thickness(2),
  119. Width = 25,
  120. BorderThickness = new Thickness(0),
  121. Background = new SolidColorBrush(Colors.White),
  122. Visibility = def.Visibility,
  123. };
  124. button.Click += (sender, args) =>
  125. {
  126. var cvt = CreateConverter();
  127. var val = cvt != null
  128. ? cvt.ConvertBack(textbox.Text, typeof(object), null, CultureInfo.CurrentCulture)
  129. : textbox.Text;
  130. val = def.DoClick(val);
  131. textbox.Text = cvt != null
  132. ? cvt.Convert(val, typeof(String), null, CultureInfo.CurrentCulture) as String ?? ""
  133. : val?.ToString() ?? "";
  134. textbox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
  135. button.Content = new Image() { Source = def.Image };
  136. button.Visibility = def.Visibility;
  137. };
  138. button.SetValue(DockPanel.DockProperty,def.Position == DynamicGridMaskColumnButtonPosition.Left ? Dock.Left : Dock.Right);
  139. dock.Children.Add(button);
  140. }
  141. }
  142. dock.Children.Add(textbox);
  143. return border;
  144. }
  145. );
  146. return new(cellTemplate, editTemplate);
  147. }
  148. protected override void Configure(GridTemplateColumn column, TEditor editor)
  149. {
  150. var (cellTemplate, editTemplate) = CreateTemplates(column, editor, MappingName);
  151. column.CellTemplate = cellTemplate;
  152. column.EditTemplate = editTemplate;
  153. column.SetCellBoundValue = false;
  154. }
  155. protected override void Configure(TreeGridTemplateColumn column, TEditor editor)
  156. {
  157. var (cellTemplate, editTemplate) = CreateTemplates(column, editor, TreeMappingName);
  158. column.CellTemplate = cellTemplate;
  159. column.EditTemplate = editTemplate;
  160. column.SetCellBoundValue = false;
  161. }
  162. protected virtual DynamicGridMaskColumnButton[]? CreateButtons()
  163. {
  164. return null;
  165. }
  166. private void OnTargetUpdated(object? sender, DataTransferEventArgs e)
  167. {
  168. if (sender is DockPanel dock && e.Source is TextBox textbox && e.Property == TextBox.TextProperty)
  169. {
  170. var buttons = dock?.Children.OfType<Button>().ToArray();
  171. var defs = buttons?.Select(x => x.Tag).OfType<DynamicGridMaskColumnButton>()?.ToArray();
  172. if (defs?.Any() == true)
  173. {
  174. var cvt = CreateConverter();
  175. var val = cvt != null
  176. ? cvt.ConvertBack(textbox.Text, typeof(object), null, CultureInfo.CurrentCulture)
  177. : textbox.Text;
  178. UpdateButtons(val, defs);
  179. if (buttons != null)
  180. foreach (var button in buttons)
  181. {
  182. var def = button.Tag as DynamicGridMaskColumnButton;
  183. button.Content = new Image() { Source = def?.Image };
  184. button.Visibility = def?.Visibility ?? Visibility.Collapsed;
  185. }
  186. }
  187. }
  188. }
  189. protected virtual void UpdateButtons(object? value, DynamicGridMaskColumnButton[]? buttons)
  190. {
  191. }
  192. private void OnSourceUpdated(object? sender, DataTransferEventArgs e)
  193. {
  194. if (sender is DockPanel dock && e.Source is TextBox textbox && e.Property == TextBox.TextProperty)
  195. {
  196. var buttons = dock?.Children.OfType<Button>().ToArray();
  197. SourceUpdated(textbox.Text, buttons ?? new Button[] { });
  198. }
  199. }
  200. protected virtual void SourceUpdated(string text, Button[] buttons)
  201. {
  202. }
  203. protected DynamicGridMaskColumn(DynamicGridColumn definition) : base(definition)
  204. {
  205. }
  206. }