Skip to content

Commit 0d1faed

Browse files
committed
get the basics
1 parent 437bd81 commit 0d1faed

File tree

12 files changed

+225
-1
lines changed

12 files changed

+225
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<EnableMSTestRunner>true</EnableMSTestRunner>
9+
<OutputType>Exe</OutputType>
10+
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
11+
<!--
12+
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
13+
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
14+
-->
15+
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
16+
</PropertyGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
20+
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.0.4" />
21+
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport" Version="1.8.5" />
22+
<PackageReference Include="MSTest" Version="3.10.5" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
27+
</ItemGroup>
28+
29+
</Project>

SimpleDataGrid.Tests/Test1.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SimpleDataGrid.Tests;
2+
3+
[TestClass]
4+
public sealed class Test1
5+
{
6+
[TestMethod]
7+
public void TestMethod1()
8+
{
9+
}
10+
}

SimpleDataGrid.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.14.36301.6 d17.14
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleDataGrid", "SimpleDataGrid\SimpleDataGrid.csproj", "{71B53291-75B2-4B5E-91B1-245E611AB4EA}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleDataGrid.Tests", "SimpleDataGrid.Tests\SimpleDataGrid.Tests.csproj", "{6CB54D0C-857F-46FB-8A0D-5719DC0718BA}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{71B53291-75B2-4B5E-91B1-245E611AB4EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{71B53291-75B2-4B5E-91B1-245E611AB4EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{71B53291-75B2-4B5E-91B1-245E611AB4EA}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{6CB54D0C-857F-46FB-8A0D-5719DC0718BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{6CB54D0C-857F-46FB-8A0D-5719DC0718BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{6CB54D0C-857F-46FB-8A0D-5719DC0718BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{6CB54D0C-857F-46FB-8A0D-5719DC0718BA}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
using SimpleDataGrid.Pagination;
4+
5+
namespace SimpleDataGrid.Controls;
6+
7+
public class PagedDataGrid : DataGrid
8+
{
9+
public static readonly DependencyProperty PagedSourceProperty =
10+
DependencyProperty.Register(nameof(PagedSource), typeof(object), typeof(PagedDataGrid),
11+
new PropertyMetadata(null, OnPagedSourceChanged));
12+
13+
public object? PagedSource
14+
{
15+
get => GetValue(PagedSourceProperty);
16+
set => SetValue(PagedSourceProperty, value);
17+
}
18+
19+
private static void OnPagedSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
20+
{
21+
if (d is PagedDataGrid grid && e.NewValue is IPagedSource paged)
22+
{
23+
grid.ItemsSource = paged.CurrentPageItems;
24+
}
25+
}
26+
}
27+
28+
public interface IPagedSource
29+
{
30+
object CurrentPageItems { get; }
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SimpleDataGrid.Pagination;
8+
internal class DataGridExtensions
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SimpleDataGrid.Pagination;
8+
internal class FilterCollection
9+
{
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SimpleDataGrid.Filtering;
2+
3+
public class FilterDescriptor<T>(Func<T, bool> predicate)
4+
{
5+
public Func<T, bool> Predicate { get; } = predicate ?? throw new ArgumentNullException(nameof(predicate));
6+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System.ComponentModel;
2+
3+
namespace SimpleDataGrid.Pagination;
4+
5+
public class PagedCollection<T> : INotifyPropertyChanged
6+
{
7+
private readonly int _pageSize;
8+
private int _currentPage;
9+
private IReadOnlyList<T> _source = Array.Empty<T>();
10+
private IReadOnlyList<T> _filtered = Array.Empty<T>();
11+
12+
private Func<T, bool> _filter = _ => true;
13+
private Func<T, string>? _searchSelector;
14+
private string? _searchTerm;
15+
16+
public PagedCollection(int pageSize = 50)
17+
{
18+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pageSize);
19+
this._pageSize = pageSize;
20+
}
21+
22+
public void SetSource(IReadOnlyList<T> items)
23+
{
24+
_source = items ?? throw new ArgumentNullException(nameof(items));
25+
ApplyFiltering();
26+
}
27+
28+
public void SetFilter(Func<T, bool> filter)
29+
{
30+
this._filter = filter ?? (_ => true);
31+
ApplyFiltering();
32+
}
33+
34+
public void SetSearch(Func<T, string> selector, string? term)
35+
{
36+
_searchSelector = selector;
37+
_searchTerm = term;
38+
ApplyFiltering();
39+
}
40+
41+
private void ApplyFiltering()
42+
{
43+
var query = _source.Where(_filter);
44+
45+
if (!string.IsNullOrWhiteSpace(_searchTerm) && _searchSelector != null)
46+
{
47+
query = query.Where(x =>
48+
_searchSelector(x)
49+
?.Contains(_searchTerm, StringComparison.OrdinalIgnoreCase) == true);
50+
}
51+
52+
_filtered = query.ToList();
53+
_currentPage = 0;
54+
RaiseAllChanged();
55+
}
56+
57+
public IReadOnlyList<T> CurrentPageItems =>
58+
_filtered.Skip(_currentPage * _pageSize).Take(_pageSize).ToList();
59+
60+
public int CurrentPage => _currentPage + 1;
61+
62+
public int TotalPages => (int)Math.Ceiling((double)_filtered.Count / _pageSize);
63+
64+
public bool HasNext => _currentPage < TotalPages - 1;
65+
public bool HasPrevious => _currentPage > 0;
66+
67+
public void NextPage()
68+
{
69+
if (HasNext)
70+
{
71+
_currentPage++;
72+
OnPropertyChanged(nameof(CurrentPageItems));
73+
OnPropertyChanged(nameof(CurrentPage));
74+
OnPropertyChanged(nameof(HasNext));
75+
OnPropertyChanged(nameof(HasPrevious));
76+
}
77+
}
78+
79+
public void PreviousPage()
80+
{
81+
if (HasPrevious)
82+
{
83+
_currentPage--;
84+
OnPropertyChanged(nameof(CurrentPageItems));
85+
OnPropertyChanged(nameof(CurrentPage));
86+
OnPropertyChanged(nameof(HasNext));
87+
OnPropertyChanged(nameof(HasPrevious));
88+
}
89+
}
90+
91+
private void RaiseAllChanged()
92+
{
93+
OnPropertyChanged(nameof(CurrentPageItems));
94+
OnPropertyChanged(nameof(CurrentPage));
95+
OnPropertyChanged(nameof(TotalPages));
96+
OnPropertyChanged(nameof(HasNext));
97+
OnPropertyChanged(nameof(HasPrevious));
98+
}
99+
100+
public event PropertyChangedEventHandler? PropertyChanged;
101+
private void OnPropertyChanged(string name) =>
102+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
103+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SimpleDataGrid.Pagination;
8+
internal class PagedCollectionView
9+
{
10+
}

0 commit comments

Comments
 (0)