FilterButton.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using InABox.Configuration;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. namespace InABox.Wpf;
  14. public class FilterButton<T> : Button
  15. {
  16. public event DynamicGridFilterComponent<T>.FilterSelectedHandler? OnFiltersSelected
  17. {
  18. add { Component.OnFiltersSelected += value; }
  19. remove { Component.OnFiltersSelected -= value; }
  20. }
  21. public event DynamicGridFilterComponent<T>.FilterRefreshHandler? OnFilterRefresh
  22. {
  23. add { Component.OnFilterRefresh += value; }
  24. remove { Component.OnFilterRefresh -= value; }
  25. }
  26. public List<DynamicGridFilterComponentBuiltInFilter<T>> BuiltInFilters => Component.BuiltInFilters;
  27. private class ButtonComponent : DynamicGridFilterComponent<T>
  28. {
  29. private FilterButton<T> Button;
  30. public ButtonComponent(
  31. FilterButton<T> button,
  32. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  33. IConfiguration<CoreFilterDefinitions> userConfiguration) : base(globalConfiguration, userConfiguration)
  34. {
  35. Button = button;
  36. }
  37. protected override void UpdateButtonText(System.Drawing.Bitmap image, string text)
  38. {
  39. Button.Update(image, text);
  40. }
  41. public void Click()
  42. {
  43. DoFilter();
  44. }
  45. }
  46. private ButtonComponent Component;
  47. public FilterButton(
  48. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  49. IConfiguration<CoreFilterDefinitions> userConfiguration)
  50. {
  51. Component = new(this, globalConfiguration, userConfiguration);
  52. SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  53. SetValue(BorderThicknessProperty, new Thickness(0.75));
  54. Height = 30;
  55. Padding = new(2);
  56. Update(Wpf.Resources.filter, "");
  57. }
  58. public void SetSettings(DynamicGridSelectedFilterSettings settings, bool refresh)
  59. {
  60. Component.SetSettings(settings, refresh);
  61. }
  62. private void Update(System.Drawing.Bitmap image, string text)
  63. {
  64. var img = new Image
  65. {
  66. Source = image.AsBitmapImage(),
  67. Margin = new()
  68. };
  69. if (!string.IsNullOrEmpty(text))
  70. {
  71. var stackPnl = new StackPanel();
  72. stackPnl.Orientation = Orientation.Horizontal;
  73. //stackPnl.Margin = new Thickness(2);
  74. stackPnl.MaxWidth = double.MaxValue;
  75. var lbl = new Label();
  76. lbl.Content = text;
  77. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  78. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  79. lbl.Margin = new Thickness(2, 0, 0, 0);
  80. lbl.ToolTip = ToolTip;
  81. stackPnl.Children.Add(img);
  82. stackPnl.Children.Add(lbl);
  83. Content = stackPnl;
  84. }
  85. else
  86. {
  87. Content = img;
  88. }
  89. }
  90. protected override void OnClick()
  91. {
  92. base.OnClick();
  93. Component.Click();
  94. }
  95. public Filter<T>? GetFilter()
  96. {
  97. return Component.GetFilter();
  98. }
  99. }