FilterButton.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. Update(Wpf.Resources.filter, "");
  55. }
  56. public void SetSettings(DynamicGridSelectedFilterSettings settings, bool refresh)
  57. {
  58. Component.SetSettings(settings, refresh);
  59. }
  60. private void Update(System.Drawing.Bitmap image, string text)
  61. {
  62. var stackPnl = new StackPanel();
  63. stackPnl.Orientation = Orientation.Horizontal;
  64. //stackPnl.Margin = new Thickness(2);
  65. if (image != null)
  66. {
  67. var img = new Image
  68. {
  69. Source = image.AsBitmapImage(),
  70. Margin = new Thickness(2)
  71. };
  72. stackPnl.Children.Add(img);
  73. }
  74. if (!string.IsNullOrEmpty(text))
  75. {
  76. stackPnl.MaxWidth = double.MaxValue;
  77. var lbl = new Label();
  78. lbl.Content = text;
  79. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  80. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  81. lbl.Margin = new Thickness(2, 0, 5, 0);
  82. lbl.ToolTip = ToolTip;
  83. stackPnl.Children.Add(lbl);
  84. }
  85. else
  86. {
  87. stackPnl.MaxWidth = 30;
  88. }
  89. Content = stackPnl;
  90. }
  91. protected override void OnClick()
  92. {
  93. base.OnClick();
  94. Component.Click();
  95. }
  96. public Filter<T>? GetFilter()
  97. {
  98. return Component.GetFilter();
  99. }
  100. }