Skip to content

Commit

Permalink
Merge pull request #24 from giard-alexandre/fix/filtering-crash
Browse files Browse the repository at this point in the history
Fix/filtering crash
  • Loading branch information
giard-alexandre authored Oct 7, 2024
2 parents e4f7049 + 10a04ee commit dd8e174
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
31 changes: 28 additions & 3 deletions samples/SampleApp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
using System;
using System.Collections.ObjectModel;
using System.Reactive.Linq;

using Bogus;
using Bogus.DataSets;

using DynamicData;
using DynamicData.Binding;

using DynamicTreeDataGrid;
using DynamicTreeDataGrid.Columns;

using ReactiveUI;

namespace SampleApp.ViewModels;

public class MainWindowViewModel : ViewModelBase {
public class MainWindowViewModel : ReactiveObject {
public DynamicFlatTreeDataGridSource<Person, int> DataSource { get; set; }

private string? _filterText;
public string? FilterText
{
get => _filterText;
set => this.RaiseAndSetIfChanged(ref _filterText, value);
}

public MainWindowViewModel() {
//Set the randomizer seed if you wish to generate repeatable data sets.
//Set the randomizer seed to generate repeatable data sets.
Randomizer.Seed = new Random(8675309);
var data = new ObservableCollection<Person>(new Faker<Person>()
.RuleFor(person => person.Id, faker => faker.IndexFaker)
Expand All @@ -27,7 +38,14 @@ public MainWindowViewModel() {
.RuleFor(person => person.IsChecked, faker => faker.Random.Bool())
.Generate(300)).ToObservableChangeSet(person => person.Id);

DataSource = new DynamicFlatTreeDataGridSource<Person, int>(data) {

var searchFilter = this.WhenValueChanged(t => t.FilterText)
.Throttle(TimeSpan.FromMilliseconds(500))
.Select(BuildSearchFilter);

var filteredData = data.Filter(searchFilter);

DataSource = new DynamicFlatTreeDataGridSource<Person, int>(filteredData) {
Columns = {
new DynamicTextColumn<Person, int>("Id", "Id", person => person.Id),
new DynamicTextColumn<Person, string>("Name", "Name", person => person.Name),
Expand All @@ -40,6 +58,13 @@ public MainWindowViewModel() {
},
};
}

private static Func<Person, bool> BuildSearchFilter(string? text) {
if (string.IsNullOrEmpty(text))
return _ => true;

return t => t.Name.Contains(text, StringComparison.OrdinalIgnoreCase);
}
}

public record Person {
Expand Down
5 changes: 0 additions & 5 deletions samples/SampleApp/ViewModels/ViewModelBase.cs

This file was deleted.

4 changes: 4 additions & 0 deletions samples/SampleApp/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
</Design.DataContext>

<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Label Target="FilterTextBox">Filter Name: </Label>
<TextBox Name="FilterTextBox" Text="{Binding FilterText}" Width="200" />
</StackPanel>
<dynamicTreeDataGrid:DynamicTreeDataGrid Source="{Binding DataSource}">
<TreeDataGrid.Resources>
<DataTemplate x:Key="HeightCell" DataType="{x:Type viewModels:Person}">
Expand Down
1 change: 1 addition & 0 deletions src/DynamicTreeDataGrid/DynamicFlatTreeDataGridSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public DynamicFlatTreeDataGridSource(IObservable<IChangeSet<TModel, TModelKey>>
FilteredCount = filteredChanges.Count();

var disposable = filteredChanges.Sort(_sort) // Use SortAndBind?
.DeferUntilLoaded()
.Bind(out _items)
.DisposeMany()
.Subscribe();
Expand Down

0 comments on commit dd8e174

Please sign in to comment.