保存 Silverlight DataGrid 排序
我有一个 Silverlight 2.0 DataGrid,其中包含需要按一定时间间隔刷新的项目列表,以显示最新信息。 当屏幕向当前用户显示时,该网格中的某些项目可以添加或删除。 例如:
如果网格如下所示,则一分钟后需要刷新数据,因为另一个用户添加了第四个条目。 (注意排序是按姓氏升序)
然后,当我获取新数据集并将其设置为数据网格的 DataSource 属性时,它会根据我的第一列重新排列数组,如下所示:
数据源更新后有没有办法重新应用排序? 我想保存数据网格按姓氏排序的事实,然后更新数据源,然后将排序重新应用到数据网格。 因此,最终数据网格将如下所示:
这些屏幕截图当然不是 Silverlight 数据网格,但这是为了简单地解释情况。
I have a Silverlight 2.0 DataGrid that contains a list of items that needs to be refreshed in an interval to display the up-to-the-minute information. There are items in this grid that may be added and may be removed while the screen is being displayed to the current user. For Example:
If grid looks like the following, then after a minute the data needs to be refreshed because another user has added a 4th entry. (notice the sort is assending by Last Name)
Data Grid http://img16.imageshack.us/img16/1667/datagrid.jpg
Then when I get the new data set and set it to the datagrid's DataSource property, it resorts the array based on my first column like so:
Data Grid2 http://img19.imageshack.us/img19/1294/datagridb.jpg
Is there any way to reapply the sort after the data source has been updated? I'd like to save the fact that the data grid is sorted by last name, then update the data source, and then reapply the sort to the data grid. So, in the end the datagrid would look like the following:
Data Grid3 http://img13.imageshack.us/img13/4636/datagrid2.jpg
These screen shots are of course not a Silverlight data grid, but this is for simplicity in explaining the situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的方法可能是使用 PagedCollectionViews,尽管我自己没有对此进行测试,但这些类包含一个属性,用于存储应用于它们的不同排序方法。 我相信您可能可以从一个集合中获取当前的排序元素,连接到新集合,并将排序应用于新集合。
要更深入地讨论这一切是如何工作的,您可以按照解释 此处
Probably the best way to go about it is to use PagedCollectionViews, although I have not tested this out myself, these classes contain a property that stores the different sorting methods applied to them. I believe that you can probably grab the current sorting element from one collection, connect to the new collection, and apply the sorting to the new collection.
For a more in depth discussion on how this all works, you can follow the explanation here
我尝试使用 PagedCollectionViews 以及 CollectionViewSources (我使用新的集合来覆盖现有的源集合)。
我的解决方案不是最优雅的,但它有效。 当更新 DataGrid 的源时,我做了自己的更改检测。
换句话说,我不是盲目地覆盖源集合,而是迭代新集合,将每个项目与现有集合进行比较,并在可能的情况下更新现有项目。
希望有帮助。
I tried using PagedCollectionViews as well as CollectionViewSources (I used the new collection to overwrite the existing source collection).
My solution isn't the most elegant, but it works. When updating the source of the DataGrid I did my own change detection.
In other words, instead of blindly overwriting the source collection I iterated through the new collection, compared each item to the existing collection, and updated the existing items where possible.
Hope that helps.
在研究了如何做到这一点之后,我已经弄清楚了。 我尝试实现其他人所说的尝试,但无法让它在 Silverlight 2.0 中工作。 因此,我在 StackOverflow 和 Google 上搜索了更多答案。 ScottLogic 和 CodeProject 似乎是我一直在寻找的东西,但我无法让它们充分工作。 因此,带着一点沮丧和尝试代码,我终于找到了解决方案。
如果您用于 DataGrid ItemsSource 的集合是 ObservableCollection(Of T) ,您可以获得对对象进行排序的能力,此外您还可以在运行时动态更新集合,从而使网格随着您插入的项目而刷新。 我创建了一个 GPL 的 Google Code 项目供您查看和下载。 请注意,我在此项目中使用的是 Microsoft Silverlight 2.0 DataGrid 2008 年 12 月版本。 我也在项目页面上链接到此下载。 (所需的 dll 是从项目内的 lib 文件夹中引用的)。
享受!
Silverlight 2.0 DataGrid Sort Project on Google Code
After researching how to do this, I have figured it out. I tried to implement what the others said to try, but could not get it to work in Silverlight 2.0. Thus, I searched StackOverflow and Google for more answers. ScottLogic and CodeProject seemed to be what I was looking for but I couldn't get them to work fully. So with a bit of frustration and playing around with code, I finally arrived on the solution.
If the collection that you're using for your DataGrid's ItemsSource is ObservableCollection(Of T), you can get the ability of sorting for the object, plus you can update the collection dynamically at runtime, causing the grid to refresh with the item you insert. I've created a Google Code project that is GPL for you to checkout and download. Please note that I'm using the Microsoft Silverlight 2.0 DataGrid December 2008 Release for this project. I have linked to this download on the project page as well. (The dll's required are referenced from within a lib folder inside the project).
Enjoy!
Silverlight 2.0 DataGrid Sort Project on Google Code