DateDynamicGridColumnFilter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using Xceed.Wpf.Toolkit;
  14. namespace InABox.DynamicGrid;
  15. public class DateDynamicGridColumnFilter(IBaseDynamicGrid grid, DynamicColumnBase column) : IDynamicGridColumnFilter, INotifyPropertyChanged
  16. {
  17. public event Action<IDynamicGridColumnFilter>? FilterChanged;
  18. public event PropertyChangedEventHandler? PropertyChanged;
  19. private DateTime? _fromDate;
  20. public DateTime? FromDate
  21. {
  22. get => _fromDate;
  23. set
  24. {
  25. _fromDate = value;
  26. FilterChanged?.Invoke(this);
  27. OnPropertyChanged();
  28. }
  29. }
  30. private DateTime? _toDate;
  31. public DateTime? ToDate
  32. {
  33. get => _toDate;
  34. set
  35. {
  36. _toDate = value;
  37. FilterChanged?.Invoke(this);
  38. OnPropertyChanged();
  39. }
  40. }
  41. public void ClearFilter()
  42. {
  43. _fromDate = null;
  44. _toDate = null;
  45. FilterChanged?.Invoke(this);
  46. }
  47. public FrameworkElement CreateControl()
  48. {
  49. var grid = new Grid();
  50. grid.AddColumn(GridUnitType.Star);
  51. grid.AddColumn(GridUnitType.Auto);
  52. grid.AddRow(GridUnitType.Auto);
  53. grid.AddRow(GridUnitType.Auto);
  54. grid.AddChild(new Label
  55. {
  56. Content = "From: ",
  57. VerticalAlignment = VerticalAlignment.Center
  58. }, 0, 0);
  59. var fromPicker = new DateTimePicker
  60. {
  61. Format = DateTimeFormat.Custom,
  62. FormatString = "dd MMM yyyy HH:mm",
  63. VerticalAlignment = VerticalAlignment.Stretch,
  64. VerticalContentAlignment = VerticalAlignment.Center,
  65. HorizontalContentAlignment = HorizontalAlignment.Center,
  66. CalendarDisplayMode = CalendarMode.Month,
  67. ShowButtonSpinner = false,
  68. Height = 25,
  69. Width = 150
  70. };
  71. fromPicker.Bind(DateTimePicker.ValueProperty, this, x => x.FromDate, mode: BindingMode.TwoWay);
  72. grid.AddChild(fromPicker, 0, 1);
  73. grid.AddChild(new Label
  74. {
  75. Content = "To: ",
  76. VerticalAlignment = VerticalAlignment.Center,
  77. Margin = new(0, 5, 0, 0)
  78. }, 1, 0);
  79. var toPicker = new DateTimePicker
  80. {
  81. Format = DateTimeFormat.Custom,
  82. FormatString = "dd MMM yyyy HH:mm",
  83. VerticalAlignment = VerticalAlignment.Stretch,
  84. VerticalContentAlignment = VerticalAlignment.Center,
  85. HorizontalContentAlignment = HorizontalAlignment.Center,
  86. CalendarDisplayMode = CalendarMode.Month,
  87. ShowButtonSpinner = false,
  88. Width = 150,
  89. Height = 25,
  90. Margin = new(0, 5, 0, 0)
  91. };
  92. toPicker.Bind(DateTimePicker.ValueProperty, this, x => x.ToDate, mode: BindingMode.TwoWay);
  93. grid.AddChild(toPicker, 1, 1);
  94. return grid;
  95. }
  96. public bool FilterRow(CoreRow row)
  97. {
  98. if(grid.GetData(row, column) is DateTime date)
  99. {
  100. return (_fromDate is null || date >= _fromDate.Value)
  101. && (_toDate is null || date <= _toDate.Value);
  102. }
  103. else
  104. {
  105. return true;
  106. }
  107. }
  108. public bool IsFiltered()
  109. {
  110. return _fromDate is not null || _toDate is not null;
  111. }
  112. private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  113. {
  114. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  115. }
  116. }