Skip to content

Commit

Permalink
Fixed an issue with the EditableCollectionView property not firing Pr…
Browse files Browse the repository at this point in the history
…opertyChanged event when it is changed
  • Loading branch information
stewienj committed Oct 4, 2020
1 parent dc497a0 commit 293d834
Show file tree
Hide file tree
Showing 10 changed files with 582 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@
<!-- DataGrid binds to TestCollection.EditableCollectionView -->
<DataGrid ItemsSource="{Binding TestCollection.EditableCollectionView}" AutoGenerateColumns="True" Grid.Row="1" Grid.Column="1" />

<Button Command="{Binding AddRandomItemCommand}" Grid.Row="3">Add Random Item</Button>

</Grid>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public partial class DataGridAndListViewControl : UserControl
{
public DataGridAndListViewControl()
{
DataContext = new DataGridTestViewModel();
InitializeComponent();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<UserControl x:Class="EditableDataGridTest.Controls.DataGridAndListViewControlAlternateBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:EditableDataGridTest.Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Center" Grid.Column="0">ListView Bound To TestCollectionView</TextBlock>

<TextBlock HorizontalAlignment="Center" Grid.Column="1">DataGrid Bound To EditableTestCollectionView</TextBlock>

<!-- ListView binds to TestCollection.CollectionView -->
<ListView ItemsSource="{Binding TestCollectionView}" Grid.Row="1" Grid.Column="0" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Label" DisplayMemberBinding="{Binding Label}"/>
<GridViewColumn Header="Value1" DisplayMemberBinding="{Binding Value1}"/>
<GridViewColumn Header="Value2" DisplayMemberBinding="{Binding Value2}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

<!-- DataGrid binds to TestCollection.EditableCollectionView -->
<DataGrid ItemsSource="{Binding EditableTestCollectionView}" AutoGenerateColumns="True" Grid.Row="1" Grid.Column="1" />

<Button Command="{Binding AddRandomItemCommand}" Grid.Row="3">Add Random Item</Button>

</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using EditableDataGridTest.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EditableDataGridTest.Controls
{
/// <summary>
/// Interaction logic for DataGridAndListViewControl.xaml
/// </summary>
public partial class DataGridAndListViewControlAlternateBinding : UserControl
{
public DataGridAndListViewControlAlternateBinding()
{
InitializeComponent();
}
}
}
9 changes: 8 additions & 1 deletion ExamplesAndTests/EditableDataGridTest/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
mc:Ignorable="d"
Title="Editable DataGrid Test" Height="400" Width="600">
<Grid>
<local:DataGridAndListViewControl/>
<TabControl>
<TabItem Header="Binding To Collection">
<local:DataGridAndListViewControl/>
</TabItem>
<TabItem Header="Alternate Binding Method">
<local:DataGridAndListViewControlAlternateBinding/>
</TabItem>
</TabControl>
</Grid>
</Window>
6 changes: 4 additions & 2 deletions ExamplesAndTests/EditableDataGridTest/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using EditableDataGridTest.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -22,7 +23,8 @@ public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new DataGridTestViewModel();
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,88 @@
using Swordfish.NET.Collections;
using Swordfish.NET.Collections.Auxiliary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace EditableDataGridTest.ViewModels
{
/// <summary>
/// View model used as a source of items for both a ListView and a DataGrid
/// </summary>
public class DataGridTestViewModel
public class DataGridTestViewModel : INotifyPropertyChanged
{
private Random _random = new Random();
public DataGridTestViewModel()
{
var randomItems = Enumerable.Range(0, 10).Select(i => new TestItem
TestCollection.PropertyChanged += (s, e) =>
{
Label = _random.Next().ToString(),
Value1 = _random.Next().ToString(),
Value2 = _random.Next().ToString()
});
switch (e.PropertyName)
{
case nameof(ConcurrentObservableCollection<TestItem>.CollectionView):
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(nameof(TestCollectionView)));
break;
case nameof(ConcurrentObservableCollection<TestItem>.EditableCollectionView):
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EditableTestCollectionView)));
break;
}
};

var randomItems = Enumerable.Range(0, 10).Select(i => GetRandomItem());
TestCollection.AddRange(randomItems);
}

private TestItem GetRandomItem() => new TestItem
{
Label = _random.Next().ToString(),
Value1 = _random.Next().ToString(),
Value2 = _random.Next().ToString()
};

private RelayCommandFactory _addRandomItemCommand = new RelayCommandFactory();
public ICommand AddRandomItemCommand => _addRandomItemCommand.GetCommand(() => TestCollection.Add(GetRandomItem()));

/// <summary>
/// The source collection
/// </summary>
public ConcurrentObservableCollection<TestItem> TestCollection { get; }
= new ConcurrentObservableCollection<TestItem>();

public IList<TestItem> TestCollectionView => TestCollection.CollectionView;
public IList<TestItem> EditableTestCollectionView => TestCollection.EditableCollectionView;

public event PropertyChangedEventHandler PropertyChanged;
}

public class ExampleViewModel : INotifyPropertyChanged
{
public ExampleViewModel()
{
TestCollection.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(ConcurrentObservableCollection<TestItem>.CollectionView):
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TestCollectionView)));
break;
case nameof(ConcurrentObservableCollection<TestItem>.EditableCollectionView):
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EditableTestCollectionView)));
break;
}
};
}

public ConcurrentObservableCollection<TestItem> TestCollection { get; }
= new ConcurrentObservableCollection<TestItem>();

public IList<TestItem> TestCollectionView => TestCollection.CollectionView;
public IList<TestItem> EditableTestCollectionView => TestCollection.EditableCollectionView;

public event PropertyChangedEventHandler PropertyChanged;
}

/// <summary>
/// Test item used to populate the test collection
/// </summary>
Expand Down
Loading

0 comments on commit 293d834

Please sign in to comment.