【问题标题】:wpf datagrid resort bugwpf 数据网格度假村错误
【发布时间】:2012-12-30 21:12:23
【问题描述】:

我被困在这里 3 周了,我快死了 :-)

当我刷新我的收藏时,SortDescription 不起作用。这是我在互联网上阅读的 wpf 中的一个已知错误。

但我似乎无法让它工作......我的经验似乎并不那么远:-) 有人可以帮我看看如何重新排序我的数据网格...

xaml:

<XmlDataProvider x:Key="SmsData" XPath="/response/conv/sms" >

    </XmlDataProvider>

    <local:RowColorConverter x:Key="RowColorConverter" />

    <CollectionViewSource x:Key="SmsView"  Source="{StaticResource SmsData}" >
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="date" Direction="Descending" />
            <scm:SortDescription PropertyName="time" Direction="Descending" />

        </CollectionViewSource.SortDescriptions>

        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="recipient" />

        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>


   ...

   <DataGrid  ItemsSource="{Binding}" DataContext="{StaticResource SmsView}"   Name="dataGrid1">

    ...

我正在通过代码绑定 dataprovider,这是我从 Internet url 获取的 xml 文件。

cs:

     public void makeConnection()
{

            ...

            XmlDocument doc = new XmlDocument();
            doc.Load(readStream);

            //refreshing my data
            xdp = FindResource("SmsData") as XmlDataProvider;
            xdp.Document = doc;

            //re-sort => this not working
            if (dataGrid1.ItemsSource != null)
            {

               CollectionViewSource cvs = FindResource("SmsView") as CollectionViewSource;

               cvs.GroupDescriptions.Clear();
               cvs.GroupDescriptions.Add(new PropertyGroupDescription("recipient"));
               cvs.SortDescriptions.Clear();
               cvs.SortDescriptions.Add(new SortDescription("date",ListSortDirection.Descending));
       cvs.SortDescriptions.Add(new SortDescription("time",ListSortDirection.Descending));
               cvs.View.Refresh();
                 }

}

【问题讨论】:

  • 您确定正在执行 if (dataGrid1.ItemsSource != null) {...} 中的代码吗?如果尚未为网格设置 ItemsSource,它将永远不会刷新。很抱歉没有将其发布为答案,但我没有“dataGrid1”的代码(也请发布)。让我知道这是否有帮助,问候
  • 是的,代码正在到达,因为我尝试使用消息框,如果我刷新它就会显示 :-) 我用 dataGrid1 的代码更新了我的帖子
  • 这有点远,但是当您遇到困难时,一切都值得一试:在您的 C# 代码中创建一个 ListCollectionView 并使用 xml 文档填充它。将 ListCollectionView 设为公共并为其提供 get 和 set 访问器,然后将 DataGrid 与 ItemsSource="{Binding SmsListCollectionView}" 绑定。一旦我使用了 CollectionViewSource 并且 GetDefaultView 方法返回了一个不可排序的视图。我将所有内容都切换到 ListCollectionView,从那以后我就没有问题了。
  • 嗨!我今天早上试图实施。这个解决方案有效!但是..要刷新我,dataContext = null 然后将我的集合召回到dataContext中。这不是一个坏方法吗?因为那样,我每次刷新都会重新制作整个数据网格......
  • 在我的拙见中,将 datacontext 设置为 null 然后再次设置到 ListCollectionView 也不错(尽管 ListCollectionView 应该通知网格集合中的更改......)。如果您不介意,我会将我之前的评论作为答案发布,以便您批准,好吗?

标签: wpf datagrid collectionview


【解决方案1】:

在您的 C# 代码中创建一个 ListCollectionView 并使用 xml 文档填充它。将 ListCollectionView 设为公共并为其提供 get 和 set 访问器,然后将 DataGrid 与 ItemsSource="{Binding SmsListCollectionView}" 绑定。一旦我使用了 CollectionViewSource 并且 GetDefaultView 方法返回了一个不可排序的视图。我将所有内容都切换到 ListCollectionView,从那以后我就没有问题了。

关于刷新,我觉得你还可以,see this SO question and its solution.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 2014-02-08
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2012-09-05
    相关资源
    最近更新 更多