我的应用程序客户端/服务器有问题。我使用 MVVM 模式。在我看来,我在 ViewModel 中使用以下代码行DataGrid
绑定了 a :ICollection
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));
一开始,这两行工作正常:我DataGrid
有我的客户列表,排序没问题。
但是当服务器更新我的客户列表时,我想更新我的收藏,所以,我的DataGrid
. 因此,我收到了新客户列表的通知。当我收到此通知时,我用这些行更新我的收藏:
Customers = CollectionViewSource.GetDefaultView(e.CustomersInfo);
_customers.SortDescriptions.Clear();
_customers .SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));
Customers.Refresh();
在这里,我DataGrid
的数据很好刷新,但排序不是刷新,因为一开始,我的客户列表是按 CreationDate 排序的,但刷新后,我的列表是按客户名称排序的。
在这里,我的 XAML 代码:
<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>
你有什么想法可以帮助我吗?经过一些研究,我找到了任何解决方案......