add four DataGrid in sandbox-WpfApp

This commit is contained in:
zerodev1200 2024-10-17 23:28:25 +09:00
parent 62b959c2c2
commit e7e9145011
2 changed files with 156 additions and 22 deletions

View File

@ -5,19 +5,20 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<!--<Grid>
Title="MainWindow" Height="800" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding ItemsView}"></ListView>
<Button Grid.Column="1" Click="Button_Click">Insert</Button>
</Grid>-->
<StackPanel>
<!-- 1つ目のグループ (上部に配置) -->
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Vertical">
<ListBox ItemsSource="{Binding ItemsView}" />
<Button Content="Add" Command="{Binding AddCommand}" />
<Button Content="AddRange" Command="{Binding AddRangeCommand}" />
@ -27,7 +28,39 @@
<Button Content="Clear" Command="{Binding ClearCommand}" />
<Button Content="Reverse" Command="{Binding ReverseCommand}" />
<Button Content="Sort" Command="{Binding SortCommand}" />
<Button Content="AttachFilter" Command="{Binding AttachFilterCommand}" />
<Button Content="ResetFilter" Command="{Binding ResetFilterCommand}" />
</StackPanel>
<!-- 左上 (NotWritable, NonFilter) -->
<GroupBox Grid.Row="1" Grid.Column="0" Header="NotWritable, NonFilter">
<StackPanel Orientation="Vertical">
<DataGrid ItemsSource="{Binding NotWritableNonFilterView}" />
</StackPanel>
</GroupBox>
<!-- 右上 (Writable, NonFilter) -->
<GroupBox Grid.Row="1" Grid.Column="1" Header="Writable, NonFilter">
<StackPanel Orientation="Vertical">
<DataGrid ItemsSource="{Binding WritableNonFilterPersonView}" />
</StackPanel>
</GroupBox>
<!-- 左下 (NotWritable, Filter) -->
<GroupBox Grid.Row="2" Grid.Column="0" Header="NotWritable, Filter">
<StackPanel Orientation="Vertical">
<DataGrid ItemsSource="{Binding NotWritableFilterView}" />
<Button Content="AttachFilter" Command="{Binding AttachFilterCommand2}" />
<Button Content="ResetFilter" Command="{Binding ResetFilterCommand2}" />
</StackPanel>
</GroupBox>
<!-- 右下 (Writable, Filter) -->
<GroupBox Grid.Row="2" Grid.Column="1" Header="Writable, Filter">
<StackPanel Orientation="Vertical">
<DataGrid ItemsSource="{Binding WritableFilterPersonView}" />
<Button Content="AttachFilter" Command="{Binding AttachFilterCommand3}" />
<Button Content="ResetFilter" Command="{Binding ResetFilterCommand3}" />
</StackPanel>
</GroupBox>
</Grid>
</Window>

View File

@ -87,6 +87,18 @@ namespace WpfApp
public ReactiveCommand<Unit> AttachFilterCommand { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ResetFilterCommand { get; } = new ReactiveCommand<Unit>();
private ObservableList<Person> SourceList { get; } = [];
private IWritableSynchronizedView<Person, Person> writableFilter;
private ISynchronizedView<Person, Person> notWritableFilter;
public NotifyCollectionChangedSynchronizedViewList<Person> NotWritableNonFilterView { get; }
public NotifyCollectionChangedSynchronizedViewList<Person> NotWritableFilterView { get; }
public NotifyCollectionChangedSynchronizedViewList<Person> WritableNonFilterPersonView { get; }
public NotifyCollectionChangedSynchronizedViewList<Person> WritableFilterPersonView { get; }
public ReactiveCommand<Unit> AttachFilterCommand2 { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ResetFilterCommand2 { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> AttachFilterCommand3 { get; } = new ReactiveCommand<Unit>();
public ReactiveCommand<Unit> ResetFilterCommand3 { get; } = new ReactiveCommand<Unit>();
public ViewModel()
{
observableList.Add(1);
@ -156,8 +168,97 @@ namespace WpfApp
{
view.ResetFilter();
});
SourceList.Add(new() { Name = "a", Age = 1 });
SourceList.Add(new() { Name = "b", Age = 2 });
SourceList.Add(new() { Name = "c", Age = 3 });
SourceList.Add(new() { Name = "d", Age = 4 });
//NotWritable, NonFilter
NotWritableNonFilterView = SourceList.ToNotifyCollectionChanged();
//NotWritable, Filter
notWritableFilter = SourceList.CreateView(x => x);
NotWritableFilterView = notWritableFilter.ToNotifyCollectionChanged();
//Writable, NonFilter
WritableNonFilterPersonView = SourceList.ToWritableNotifyCollectionChanged();
//WritableNonFilterPersonView = SourceList.ToWritableNotifyCollectionChanged(x => x, (Person newView, Person original, ref bool setValue) =>
//{
// if (setValue)
// {
// // default setValue == true is Set operation
// original.Name = newView.Name;
// original.Age = newView.Age;
// // You can modify setValue to false, it does not set original collection to new value.
// // For mutable reference types, when there is only a single,
// // bound View and to avoid recreating the View, setting false is effective.
// // Otherwise, keeping it true will set the value in the original collection as well,
// // and change notifications will be sent to lower-level Views(the delegate for View generation will also be called anew).
// setValue = false;
// return original;
// }
// else
// {
// // default setValue == false is Add operation
// return new Person { Age = newView.Age, Name = newView.Name };
// }
//}, null);
//Writable, Filter
writableFilter = SourceList.CreateWritableView(x => x);
WritableFilterPersonView = writableFilter.ToWritableNotifyCollectionChanged();
//WritableFilterPersonView = writableFilter.ToWritableNotifyCollectionChanged((Person newView, Person original, ref bool setValue) =>
//{
// if (setValue)
// {
// // default setValue == true is Set operation
// original.Name = newView.Name;
// original.Age = newView.Age;
// // You can modify setValue to false, it does not set original collection to new value.
// // For mutable reference types, when there is only a single,
// // bound View and to avoid recreating the View, setting false is effective.
// // Otherwise, keeping it true will set the value in the original collection as well,
// // and change notifications will be sent to lower-level Views(the delegate for View generation will also be called anew).
// setValue = false;
// return original;
// }
// else
// {
// // default setValue == false is Add operation
// return new Person { Age = newView.Age, Name = newView.Name };
// }
//});
AttachFilterCommand2.Subscribe(_ =>
{
notWritableFilter.AttachFilter(x => x.Age % 2 == 0);
});
ResetFilterCommand2.Subscribe(_ =>
{
notWritableFilter.ResetFilter();
});
AttachFilterCommand3.Subscribe(_ =>
{
writableFilter.AttachFilter(x => x.Age % 2 == 0);
});
ResetFilterCommand3.Subscribe(_ =>
{
writableFilter.ResetFilter();
});
}
}
public class Person
{
public int? Age { get; set; }
public string? Name { get; set; }
}
public class WpfDispatcherCollection(Dispatcher dispatcher) : ICollectionEventDispatcher
{