WPF DataGrid列标题排序箭头在更改其视图集合源后消失

时间:2018-08-17 07:34:52

标签: c# wpf wpfdatagrid collectionviewsource

我有以下简单的代码可以重现此问题:

XAML:

config.saml_configure do |settings|
  # assertion_consumer_service_url is required starting with ruby-saml 1.4.3: https://github.com/onelogin/ruby-saml#updating-from-142-to-143
  settings.assertion_consumer_service_url     = "http://localhost:3000/users/saml/auth"
  settings.assertion_consumer_service_binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
  settings.name_identifier_format             = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"
  settings.issuer                             = "http://localhost:3000/saml/metadata"
  settings.authn_context                      = ""
  settings.idp_slo_target_url                 = "http://localhost/simplesaml/www/saml2/idp/SingleLogoutService.php"
  settings.idp_sso_target_url                 = "http://localhost/simplesaml/www/saml2/idp/SSOService.php"
  settings.idp_cert_fingerprint               = "00:A1:2B:3C:44:55:6F:A7:88:CC:DD:EE:22:33:44:55:D6:77:8F:99"
  settings.idp_cert_fingerprint_algorithm     = "http://www.w3.org/2000/09/xmldsig#sha1"
end

查看模型:

<DataGrid ItemsSource="{Binding Source.View}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name"
                            Binding="{Binding Name}"
                            SortMemberPath="Name"
                            SortDirection="Ascending"/>
    </DataGrid.Columns>
</DataGrid>

以上代码的作用是,当应用启动时,使用private readonly ObservableCollection<Data> _collection1 = new ObservableCollection<Data> {new Data("item 1"), new Data("item 2")}; private readonly ObservableCollection<Data> _collection2 = new ObservableCollection<Data> {new Data("person 1"), new Data("person 2")}; public MainViewModel() { Source.Source = _collection1; // Set up a timer that runs 5 seconds. Observable.Timer(TimeSpan.FromSeconds(5)).ObserveOn(AsyncOperationManager.SynchronizationContext).Subscribe(_ => { // Get existing sort descriptions. var existingSortDescription = Source.View.SortDescriptions.ToList(); // Change source. Source.Source = _collection2; // This has to be done in order to maintain the sort order. existingSortDescription.ForEach(Source.SortDescriptions.Add); }); } public CollectionViewSource Source { get; } = new CollectionViewSource(); private class Data { public Data(string name) { Name = name; } public string Name { get; } } 作为数据网格的项目源。

5秒后,将数据网格的项目源更改为_collection1

如果运行上述代码,则将源更改为_collection2后,“名称”列标题中的排序方向箭头将消失,但排序仍然正确。

这是WPF _collection2控件中的错误,还是我在这里遗漏了某些东西?

1 个答案:

答案 0 :(得分:1)

您在视图模型中添加到SortDescriptions的{​​{1}}的{​​{1}}不会影响您在视图View控件中看到的箭头

您可以通过设置特定列的CollectionViewSource属性来以编程方式显示特定列的箭头。因此,您可以做的是创建一个自定义DataGrid控件来为您处理此操作(内置控件不会像您已经发现的那样),例如:

SortDirection

然后,您只需在XAML中将DataGrid元素替换为public class CustomDataGrid : DataGrid { protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) { base.OnItemsSourceChanged(oldValue, newValue); INotifyCollectionChanged oldView = oldValue as INotifyCollectionChanged; if (oldView != null) oldView.CollectionChanged -= View_CollectionChanged; INotifyCollectionChanged newView = newValue as INotifyCollectionChanged; if (newView != null) newView.CollectionChanged += View_CollectionChanged; } private void View_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { ICollectionView view = sender as ICollectionView; if (view != null) { SortDescription sd = view.SortDescriptions.LastOrDefault(); if (sd != null) { DataGridColumn column = Columns.FirstOrDefault(x => x.SortMemberPath == sd.PropertyName); if (column != null) { column.SortDirection = sd.Direction; } } } } } 元素即可。