FilterButton.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using InABox.Configuration;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. namespace InABox.Wpf;
  15. public class FilterButton<T> : Button
  16. {
  17. public event DynamicGridFilterComponent<T>.FilterSelectedHandler? OnFiltersSelected
  18. {
  19. add { Component.OnFiltersSelected += value; }
  20. remove { Component.OnFiltersSelected -= value; }
  21. }
  22. public event DynamicGridFilterComponent<T>.FilterRefreshHandler? OnFilterRefresh
  23. {
  24. add { Component.OnFilterRefresh += value; }
  25. remove { Component.OnFilterRefresh -= value; }
  26. }
  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. Update(Wpf.Resources.filter, "");
  56. }
  57. private void Update(System.Drawing.Bitmap image, string text)
  58. {
  59. var stackPnl = new StackPanel();
  60. stackPnl.Orientation = Orientation.Horizontal;
  61. //stackPnl.Margin = new Thickness(2);
  62. if (image != null)
  63. {
  64. var img = new Image
  65. {
  66. Source = image.AsBitmapImage(),
  67. Margin = new Thickness(2)
  68. };
  69. stackPnl.Children.Add(img);
  70. }
  71. if (!string.IsNullOrEmpty(text))
  72. {
  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, 5, 0);
  79. lbl.ToolTip = ToolTip;
  80. stackPnl.Children.Add(lbl);
  81. }
  82. else
  83. {
  84. stackPnl.MaxWidth = 30;
  85. }
  86. Content = stackPnl;
  87. }
  88. protected override void OnClick()
  89. {
  90. base.OnClick();
  91. Component.Click();
  92. }
  93. public Filter<T>? GetFilter()
  94. {
  95. return Component.GetFilter();
  96. }
  97. }