简体   繁体   中英

How to keep the current sort of a ICollection after data refresh?

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...

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

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();

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

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();
}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM