Sfoglia il codice sorgente

Changed filter for date columns on grids to use a From and To box.

Kenric Nugteren 4 giorni fa
parent
commit
c835a73f09

+ 129 - 0
inabox.wpf/DynamicGrid/ColumnFilter/DateDynamicGridColumnFilter.cs

@@ -0,0 +1,129 @@
+using InABox.Core;
+using InABox.WPF;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using Xceed.Wpf.Toolkit;
+
+namespace InABox.DynamicGrid;
+
+public class DateDynamicGridColumnFilter(IBaseDynamicGrid grid, DynamicColumnBase column) : IDynamicGridColumnFilter, INotifyPropertyChanged
+{
+    public event Action<IDynamicGridColumnFilter>? FilterChanged;
+    public event PropertyChangedEventHandler? PropertyChanged;
+
+    private DateTime? _fromDate;
+    public DateTime? FromDate
+    {
+        get => _fromDate;
+        set
+        {
+            _fromDate = value;
+            FilterChanged?.Invoke(this);
+            OnPropertyChanged();
+        }
+    }
+
+    private DateTime? _toDate;
+    public DateTime? ToDate
+    {
+        get => _toDate;
+        set
+        {
+            _toDate = value;
+            FilterChanged?.Invoke(this);
+            OnPropertyChanged();
+        }
+    }
+
+    public void ClearFilter()
+    {
+        _fromDate = null;
+        _toDate = null;
+        FilterChanged?.Invoke(this);
+    }
+
+    public FrameworkElement CreateControl()
+    {
+        var grid = new Grid();
+        grid.AddColumn(GridUnitType.Star);
+        grid.AddColumn(GridUnitType.Auto);
+
+        grid.AddRow(GridUnitType.Auto);
+        grid.AddRow(GridUnitType.Auto);
+
+        grid.AddChild(new Label
+        {
+            Content = "From: ",
+            VerticalAlignment = VerticalAlignment.Center
+        }, 0, 0);
+        var fromPicker = new DateTimePicker
+        {
+            Format = DateTimeFormat.Custom,
+            FormatString = "dd MMM yyyy HH:mm",
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            CalendarDisplayMode = CalendarMode.Month,
+            ShowButtonSpinner = false,
+            Height = 25,
+            Width = 150
+        };
+        fromPicker.Bind(DateTimePicker.ValueProperty, this, x => x.FromDate, mode: BindingMode.TwoWay);
+        grid.AddChild(fromPicker, 0, 1);
+
+        grid.AddChild(new Label
+        {
+            Content = "To: ",
+            VerticalAlignment = VerticalAlignment.Center,
+            Margin = new(0, 5, 0, 0)
+        }, 1, 0);
+        var toPicker = new DateTimePicker
+        {
+            Format = DateTimeFormat.Custom,
+            FormatString = "dd MMM yyyy HH:mm",
+            VerticalAlignment = VerticalAlignment.Stretch,
+            VerticalContentAlignment = VerticalAlignment.Center,
+            HorizontalContentAlignment = HorizontalAlignment.Center,
+            CalendarDisplayMode = CalendarMode.Month,
+            ShowButtonSpinner = false,
+            Width = 150,
+            Height = 25,
+            Margin = new(0, 5, 0, 0)
+        };
+        toPicker.Bind(DateTimePicker.ValueProperty, this, x => x.ToDate, mode: BindingMode.TwoWay);
+        grid.AddChild(toPicker, 1, 1);
+
+        return grid;
+    }
+
+    public bool FilterRow(CoreRow row)
+    {
+        if(grid.GetData(row, column) is DateTime date)
+        {
+            return (_fromDate is null || date >= _fromDate.Value)
+                && (_toDate is null || date <= _toDate.Value);
+        }
+        else
+        {
+            return true;
+        }
+    }
+
+    public bool IsFiltered()
+    {
+        return _fromDate is not null || _toDate is not null;
+    }
+
+    private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
+    {
+        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+    }
+}

+ 1 - 1
inabox.wpf/DynamicGrid/Grids/BaseDynamicGrid.cs

@@ -563,7 +563,7 @@ public abstract class BaseDynamicGrid : ContentControl, IDynamicGridUIComponentP
         {
             if(gc.Editor is DateTimeEditor || gc.Editor is DateEditor)
             {
-                return new DateTreeDynamicGridColumnFilter(this, column);
+                return new DateDynamicGridColumnFilter(this, column);
             }
             else if(gc.Editor is IntegerEditor)
             {