DynamicActionColumn.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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; } = DynamicActionColumnPosition.End;
  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; } = null;
  20. public Func<CoreRow, string[], bool>? FilterRecord { get; set; } = null;
  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; } = 0;
  26. public FrameworkElement? TextToolTip(string? text)
  27. {
  28. if (string.IsNullOrWhiteSpace(text))
  29. return null;
  30. var border = new Border
  31. {
  32. BorderBrush = new SolidColorBrush(Colors.Gray),
  33. BorderThickness = new Thickness(0.75),
  34. CornerRadius = new CornerRadius(5),
  35. Background = new SolidColorBrush(Colors.LightYellow),
  36. Padding = new Thickness(5),
  37. Child = new Label { Content = text }
  38. };
  39. return border;
  40. }
  41. public FrameworkElement? ImageToolTip(BitmapImage? image)
  42. {
  43. if (image == null)
  44. return null;
  45. var result = new Border
  46. {
  47. Background = new SolidColorBrush(Colors.LightYellow),
  48. BorderBrush = new SolidColorBrush(Colors.Gray),
  49. BorderThickness = new Thickness(0.75)
  50. };
  51. result.Child = new Image { Source = image };
  52. return result;
  53. }
  54. public abstract object? Data(CoreRow? row);
  55. }
  56. public class DynamicTextColumn : DynamicActionColumn
  57. {
  58. public delegate string GetTextDelegate(CoreRow? row);
  59. public GetTextDelegate Text { get; private set; }
  60. public Alignment Alignment { get; set; }
  61. public DynamicTextColumn(GetTextDelegate text, ActionDelegate? action = null)
  62. {
  63. Alignment = Alignment.MiddleCenter;
  64. Text = text;
  65. Action = action;
  66. VerticalHeader = false;
  67. }
  68. public override object? Data(CoreRow? row) => Text?.Invoke(row);
  69. }
  70. public class DynamicImageColumn : DynamicActionColumn
  71. {
  72. public delegate BitmapImage? GetImageDelegate(CoreRow? row);
  73. public DynamicImageColumn(GetImageDelegate image, ActionDelegate? action = null)
  74. {
  75. Image = image;
  76. Action = action;
  77. VerticalHeader = true;
  78. }
  79. public DynamicImageColumn(BitmapImage image, ActionDelegate? action = null)
  80. {
  81. Image = r => image;
  82. Action = action;
  83. }
  84. public GetImageDelegate Image { get; protected set; }
  85. public bool AllowHeaderClick { get; set; } = false;
  86. public override object? Data(CoreRow? row) => Image?.Invoke(row);
  87. }
  88. }