FilterButton.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. private class ButtonComponent : DynamicGridFilterComponent<T>
  27. {
  28. private FilterButton<T> Button;
  29. public ButtonComponent(
  30. FilterButton<T> button,
  31. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  32. IConfiguration<CoreFilterDefinitions> userConfiguration) : base(globalConfiguration, userConfiguration)
  33. {
  34. Button = button;
  35. }
  36. protected override void UpdateButtonText(System.Drawing.Bitmap image, string text)
  37. {
  38. Button.Update(image, text);
  39. }
  40. public void Click()
  41. {
  42. DoFilter();
  43. }
  44. }
  45. private ButtonComponent Component;
  46. public FilterButton(
  47. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  48. IConfiguration<CoreFilterDefinitions> userConfiguration)
  49. {
  50. Component = new(this, globalConfiguration, userConfiguration);
  51. SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  52. SetValue(BorderThicknessProperty, new Thickness(0.75));
  53. Height = 30;
  54. Padding = new(2);
  55. Update(Wpf.Resources.filter, "");
  56. }
  57. public void SetSettings(DynamicGridSelectedFilterSettings settings, bool refresh)
  58. {
  59. Component.SetSettings(settings, refresh);
  60. }
  61. private void Update(System.Drawing.Bitmap image, string text)
  62. {
  63. var img = new Image
  64. {
  65. Source = image.AsBitmapImage(),
  66. Margin = new()
  67. };
  68. if (!string.IsNullOrEmpty(text))
  69. {
  70. var stackPnl = new StackPanel();
  71. stackPnl.Orientation = Orientation.Horizontal;
  72. //stackPnl.Margin = new Thickness(2);
  73. stackPnl.MaxWidth = double.MaxValue;
  74. var lbl = new Label();
  75. lbl.Content = text;
  76. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  77. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  78. lbl.Margin = new Thickness(2, 0, 0, 0);
  79. lbl.ToolTip = ToolTip;
  80. stackPnl.Children.Add(img);
  81. stackPnl.Children.Add(lbl);
  82. Content = stackPnl;
  83. }
  84. else
  85. {
  86. Content = img;
  87. }
  88. }
  89. protected override void OnClick()
  90. {
  91. base.OnClick();
  92. Component.Click();
  93. }
  94. public Filter<T>? GetFilter()
  95. {
  96. return Component.GetFilter();
  97. }
  98. }