2

I've a problem in my application Client/Server. I work with the MVVM pattern. In my view, I've a DataGrid which is bound with a ICollection in my ViewModel with these lines of code :

public ICollectionView Customers
    {
        get
        {
            return _customers;
        }
        set
        {
            _customers= value;
            RaisePropertyChanged("Customers");
        }
    }

_customers = CollectionViewSource.GetDefaultView(Manager.Instance.Customers());
_customers .SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));

At the start, these two lines work fine : my DataGrid has my list of customers and the sort is ok.

But when the server update my list of customer, i would like to Update my collection and, so, my DataGrid. So, I received a notification with my new List of customers. When I received this notification, I update my collection with these lines :

 Customers = CollectionViewSource.GetDefaultView(e.CustomersInfo);
_customers.SortDescriptions.Clear();
_customers .SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));
Customers.Refresh();

Here, my DataGrid is well refresh with the good data, but the sort is not refresh beacause, at the beginning, my list of customers is sorted by the CreationDate, but after the refresh, my list is sorted by CustomersName.

Here, my XAML code :

<DataGrid AutoGenerateColumns="false" ItemsSource="{Binding Customers, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Name="dtgEventInfo" SelectedItem="{Binding SelectedCustomers, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource ItemContStyle}" IsEnabled="True" IsReadOnly="True" Margin="0,0,330,0" GridLinesVisibility="None" SelectionMode="Single">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}" Header="Name" MinWidth="40" Width="Auto"/>
                    <DataGridTextColumn Binding="{Binding CreationDate, Mode=TwoWay}" Header="Date" MinWidth="50" Width="Auto"/>
                </DataGrid.Columns>
</DataGrid>

Do you have any ideas to help me please ? After some researches, I've bot found any solutions...

Richard E
  • 4,819
  • 1
  • 19
  • 25

5 Answers5

0

I think you should just replace RaisePropertyChanged("Events"); by RaisePropertyChanged("Customers");

Olwaro
  • 361
  • 1
  • 8
0

when need the behavior you want, i do the follwoing:

  • i create an ObservableCollection of my type ONCE eg. _mysource
  • i create a ICollectionView ONCE eg. _myview
  • i use Clear/Add/Remove to update _mysource
  • sorting/grouping/filtering are applied, on _myview.Refresh()

so this would work for you

 this._mysource.Clear();
 this._mysource.AddRange(e.CustomersInfo);
 _myview.Refresh();
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • The refresh method is aslready call in my code. It's work for update my data, but the sort defines in my list is not keep – user1501990 Jun 24 '13 at 11:11
  • i just post the code where both work update and sorting. i dunno why sorting dont work with your solution, i will do a little testproject – blindmeis Jun 24 '13 at 11:28
  • hmm if i try it the same way like you did - and it work. did you check your e.CustomerInfo type? is it the same like your initial collection type? – blindmeis Jun 24 '13 at 11:39
  • I will try this in my ViewModel. I'll say you if it solve my problem. Thanks – user1501990 Jun 24 '13 at 12:03
  • After, after a refresh, i will update my ICollection. Now with your solution, I update an observableCollection. But now, when I try to refresh my ICollection in my viewModel, i've an error which say me that the thread in not the owner and so, I cannot update my sort now – user1501990 Jun 24 '13 at 12:46
0

You can try 2 things

  1. Implement INotifyPropertyChanged Interface in your ViewModel class which is cappable of updating UI when required. Please see the http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

  2. Use ObservableCollection instead of CollectionView, which is already implemented INotifyPropertyChanged interface and updates the UI when required. Sorting operation also possible by implementing IComparer Interface. Please see the link http://msdn.microsoft.com/en-us/library/ms668604.aspx

Please mark the answer if useful

Vimal CK
  • 3,543
  • 1
  • 26
  • 47
0

I guess your updating method is running in different thread from main thread. You can try this:

if (Application.Current.Dispatcher.CheckAccess())
{
    UpdateCollectionView(e.CustomersInfo);
}
else
{
    Application.Current.Dispatcher.Invoke(new Action(() => UpdateCollectionView(e.CustomersInfo)));
}
private void UpdateCollectionView(IEnumerable<Customer> customers)
{
    Customers = CollectionViewSource.GetDefaultView(customers);
    Customers.SortDescriptions.Clear();
    Customers.SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));
    Customers.Refresh();
}
Bill Zhang
  • 1,909
  • 13
  • 10
0

After some searches and lots of debug mode in Visual Studio, I think I've found my problem. In my manager Manager.cs, when I received my notification, I did this:

 ObservableCollection<CustomerInfo> collection = new ObservableCollection<CustomerInfo>(e.CustomersInfoList); 
CustomerListViewModel.Instance.Customers = collection; 

So, this new instantiation can probably cause my problem, because, on this same collection, I realize some filter, and, the sort is OK after the filter method!

So do you have any ideas now?

Dan D.
  • 73,243
  • 15
  • 104
  • 123