FilterButton.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. where T : BaseObject, new()
  17. {
  18. public event DynamicGridFilterComponent<T>.FilterSelectedHandler? OnFiltersSelected
  19. {
  20. add { Component.OnFiltersSelected += value; }
  21. remove { Component.OnFiltersSelected -= value; }
  22. }
  23. public event DynamicGridFilterComponent<T>.FilterRefreshHandler? OnFilterRefresh
  24. {
  25. add { Component.OnFilterRefresh += value; }
  26. remove { Component.OnFilterRefresh -= value; }
  27. }
  28. private class ButtonComponent : DynamicGridFilterComponent<T>
  29. {
  30. private FilterButton<T> Button;
  31. public ButtonComponent(
  32. FilterButton<T> button,
  33. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  34. IConfiguration<CoreFilterDefinitions> userConfiguration) : base(globalConfiguration, userConfiguration)
  35. {
  36. Button = button;
  37. }
  38. protected override void UpdateButtonText(System.Drawing.Bitmap image, string text)
  39. {
  40. Button.Update(image, text);
  41. }
  42. public void Click()
  43. {
  44. DoFilter();
  45. }
  46. }
  47. private ButtonComponent Component;
  48. public FilterButton(
  49. IConfiguration<CoreFilterDefinitions> globalConfiguration,
  50. IConfiguration<CoreFilterDefinitions> userConfiguration)
  51. {
  52. Component = new(this, globalConfiguration, userConfiguration);
  53. SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  54. SetValue(BorderThicknessProperty, new Thickness(0.75));
  55. Height = 30;
  56. Update(Wpf.Resources.filter, "");
  57. }
  58. private void Update(System.Drawing.Bitmap image, string text)
  59. {
  60. var stackPnl = new StackPanel();
  61. stackPnl.Orientation = Orientation.Horizontal;
  62. //stackPnl.Margin = new Thickness(2);
  63. if (image != null)
  64. {
  65. var img = new Image
  66. {
  67. Source = image.AsBitmapImage(),
  68. Margin = new Thickness(2)
  69. };
  70. stackPnl.Children.Add(img);
  71. }
  72. if (!string.IsNullOrEmpty(text))
  73. {
  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, 5, 0);
  80. lbl.ToolTip = ToolTip;
  81. stackPnl.Children.Add(lbl);
  82. }
  83. else
  84. {
  85. stackPnl.MaxWidth = 30;
  86. }
  87. Content = stackPnl;
  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. }