按索引对PagedCollectionView进行排序(使用Silverlight Datagrid)
我有一个silverlight数据网格,它绑定到显示RowViewModels集合的PagedCollectionView。按索引对PagedCollectionView进行排序(使用Silverlight Datagrid)
每个RowVM都有一个CellViewModels集合,datagrid列是templatecolumns,它们的内容绑定到Cell [0] .Content,Cell [1] .Content等动态生成。这是因为rowviewmodels被返回来自服务,并且可以包含任意数量的列和不同类型的内容。
这工作得很好,但是当启用对数据网格中的列进行排序时遇到了问题。看起来DataGridColumns上的SortMemberPath属性(它最终变成了一个SortDescription.PropertyName)不能用于包含索引的表达式,比如“Cells [1] .Content”。
有没有人知道解决这个问题的方法?
在这里回答我自己的问题。
我解决了这个问题,通过将我生成的列上的SortMemberPath设置为RowVM.Cells集合中的索引号,并向我的RowVM添加了SortOnMe属性。
当用户对数据网格进行排序时,它会将包含索引编号(从SortMemberPath获取)的SortDescription添加到PagedCollectionView。我通过使用下面的方法监听PagedCollectionView上的propertychanged事件来监视此情况。它添加了一个新的SortDescription,告诉PagedCollectionView在“SortOnMe”上进行排序,并复制数据以从相关单元格排序到row.SortOnMe。
private bool _currentlySorting;
private void PagedCollectionView_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var pcv = (PagedCollectionView)sender;
int columnIndex = 0;
if (!_currentlySorting && e.PropertyName == "SortDescriptions" && pcv.SortDescriptions.Count == 1 && int.TryParse(pcv.SortDescriptions[0].PropertyName, out columnIndex)) {
_currentlySorting = true;
pcv.SortDescriptions.Add(new SortDescription("SortOnMe", pcv.SortDescriptions(0).Direction));
foreach (RowViewModel row in pcv.SourceCollection) {
row.SortOnMe = row.Cells(columnIndex).Content;
}
_currentlySorting = false;
}
}
说实话,这是一个相当丑陋的解决方案。但是它很有用,而且我现在花了太多时间在我的墙上猛撞墙。
由于PagedCollectionView是一个密封类(为什么?!),我唯一能想到的做法是创建自己的PagedCollectionView并在那里处理排序。
可以基于在CellViewModels
的集合的动态对象见本
Lightweight DataTable for your Silverlight applications
最好的问候。
Bruno Rocha
嗨!感谢您的回复,但我不知道这是如何与我的问题排序silverlight工具包datagrid? – Zissou 2010-07-09 09:32:49