Edited: I created a new VS2010 WPF application with three files MainWindow.xaml, MainWindow.xaml.cs and MainWindowViewModel.cs ( in the list below ). If someone feels really helpful, you can recreate the problem in seconds (copy / paste). When you start the application, the DataGrid will display the "OldCollection" line, which is incorrect. If you change the binding of ItemsSource to MyCollection, it will display "NewCollection", which is true.
Full Description:
First I had a DataGrid with its ItemsSource associated with MyCollection. I need an UpdateCollection method that assigns a new ObservableCollection <> to MyCollection. With the addition of NotifyPropertyChange to MyCollection UI updates.
Then it became necessary to introduce a CollectionViewSource to enable grouping. When the user interface is bound to MyCollectionView, calls to UpdateCollection no longer work. The debugger confirms that MyCollectionView always contains the original MyCollection. How can I get my NewCollection to be reflected in the view? I tried View.Refresh (), Binding CollectionViewSource and many other strategies.
Note. First of all, others relate to changes in the elements of the collection that do not update the view (grouping / sorting) without calling Refresh. My problem is that I am assigning a new collection to CollectionViewSource and the view / user interface never changes.
<Window x:Class="CollectionView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="grid" ItemsSource="{Binding MyCollectionView}" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace CollectionView
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
namespace CollectionView
{
class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
MyCollection = new ObservableCollection<MyObject>() { new MyObject() { TestString = "OldCollection" } };
MyCollectionViewSource = new CollectionViewSource();
Binding MyBind = new Binding() { Source = MyCollection };
BindingOperations.SetBinding(MyCollectionViewSource, CollectionViewSource.SourceProperty, MyBind);
MyCollectionView = MyCollectionViewSource.View;
MyCollection = new ObservableCollection<MyObject>() { new MyObject() { TestString = "NewCollection" } };
}
ObservableCollection<MyObject> _MyCollection;
public ObservableCollection<MyObject> MyCollection
{
get { return _MyCollection; }
set
{
if (value != _MyCollection)
{
_MyCollection = value;
NotifyPropertyChanged("MyCollection");
}
}
}
public CollectionViewSource MyCollectionViewSource { get; private set; }
public ICollectionView MyCollectionView { get; private set; }
public void UpdateCollection(ObservableCollection<MyObject> NewCollection)
{
MyCollection = NewCollection;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
class MyObject
{
public string TestString { get; set; }
}
}
Thank,
source
share