DynamicActionColumn.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Imaging;
  6. using InABox.Core;
  7. using Image = System.Windows.Controls.Image;
  8. using Label = System.Windows.Controls.Label;
  9. namespace InABox.DynamicGrid
  10. {
  11. public abstract class DynamicActionColumn : DynamicColumnBase
  12. {
  13. public DynamicActionColumnPosition Position { get; set; }
  14. public delegate bool ActionDelegate(CoreRow? row);
  15. public delegate FrameworkElement? ActionColumnToolTip(DynamicActionColumn column, CoreRow? row);
  16. public ActionDelegate? Action { get; set; }
  17. public ActionColumnToolTip? ToolTip { get; set; }
  18. public string[]? SelectedFilters { get; set; }
  19. public string[]? Filters { get; set; }
  20. public Func<CoreRow, string[], bool>? FilterRecord { get; set; }
  21. public Func<CoreRow[]?, ContextMenu?>? ContextMenu { get; set; }
  22. public object? Tag { get; set; }
  23. public string HeaderText { get; set; }
  24. public bool VerticalHeader { get; set; }
  25. public int Width { get; set; }
  26. protected override void Init()
  27. {
  28. base.Init();
  29. Width = 0;
  30. HeaderText = " ";
  31. Filters = null;
  32. FilterRecord = null;
  33. Position = DynamicActionColumnPosition.End;
  34. }
  35. public FrameworkElement? TextToolTip(string? text)
  36. {
  37. if (string.IsNullOrWhiteSpace(text))
  38. return null;
  39. var border = new Border
  40. {
  41. BorderBrush = new SolidColorBrush(Colors.Gray),
  42. BorderThickness = new Thickness(0.75),
  43. CornerRadius = new CornerRadius(5),
  44. Background = new SolidColorBrush(Colors.LightYellow),
  45. Padding = new Thickness(5),
  46. Child = new Label { Content = text }
  47. };
  48. return border;
  49. }
  50. public FrameworkElement? ImageToolTip(BitmapImage? image)
  51. {
  52. if (image == null)
  53. return null;
  54. var result = new Border
  55. {
  56. Background = new SolidColorBrush(Colors.LightYellow),
  57. BorderBrush = new SolidColorBrush(Colors.Gray),
  58. BorderThickness = new Thickness(0.75)
  59. };
  60. result.Child = new Image { Source = image };
  61. return result;
  62. }
  63. public abstract object? Data(CoreRow? row);
  64. }
  65. public class DynamicTextColumn : DynamicActionColumn
  66. {
  67. public delegate string GetTextDelegate(CoreRow? row);
  68. public GetTextDelegate Text { get; private set; }
  69. public Alignment Alignment { get; set; }
  70. public DynamicTextColumn(GetTextDelegate text, ActionDelegate? action = null)
  71. {
  72. Alignment = Alignment.MiddleCenter;
  73. Text = text;
  74. Action = action;
  75. VerticalHeader = false;
  76. }
  77. public override object? Data(CoreRow? row) => Text?.Invoke(row);
  78. }
  79. public class DynamicImageColumn : DynamicActionColumn
  80. {
  81. public delegate BitmapImage? GetImageDelegate(CoreRow? row);
  82. public DynamicImageColumn(GetImageDelegate image, ActionDelegate? action = null)
  83. {
  84. Image = image;
  85. Action = action;
  86. VerticalHeader = true;
  87. }
  88. public DynamicImageColumn(BitmapImage image, ActionDelegate? action = null)
  89. {
  90. Image = r => image;
  91. Action = action;
  92. }
  93. public GetImageDelegate Image { get; protected set; }
  94. public bool AllowHeaderClick { get; set; }
  95. protected override void Init()
  96. {
  97. base.Init();
  98. AllowHeaderClick = false;
  99. }
  100. public override object? Data(CoreRow? row) => Image?.Invoke(row);
  101. }
  102. }