CollectionViewSource.GetDefaultView 不在 Silverlight 3 中!解决方法是什么?
CollectionViewSource.GetDefaultView()
方法在 Silverlight 3 中没有。在 WPF 中,我有这个扩展方法:
public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel,
ViewModelType collectionItem,
ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase
{
Debug.Assert(collection.Contains(collectionItem));
ICollectionView collectionView = CollectionViewSource.GetDefaultView(collection);
if(collectionView != null) collectionView.MoveCurrentTo(collectionItem);
}
如何在 Silverlight 3 中编写此方法?
The CollectionViewSource.GetDefaultView()
method is not in Silverlight 3. In WPF I have this extension method:
public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel,
ViewModelType collectionItem,
ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase
{
Debug.Assert(collection.Contains(collectionItem));
ICollectionView collectionView = CollectionViewSource.GetDefaultView(collection);
if(collectionView != null) collectionView.MoveCurrentTo(collectionItem);
}
How can this be written in Silverlight 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Silverlight 不包含默认视图的概念。当您要求 Silverlight 中的控件绑定到集合时,它确实绑定到该集合,而不是绑定到默认视图。
因此,我认为您的扩展方法无法直接且完整地移植。需要对 MVVM 实现进行一些重新设计。我之前没有遇到过视图模型实例集合的概念,所以我不确定什么适合您的情况。
我见过的几种使用
CollectionViewSource
的方法是在 Xaml 中定义CollectionViewSource
并将其Source
绑定到 ViewModel 中的某些内容。或者让 ViewModel 公开一个CollectionViewSource
属性,并将 View xaml 绑定到其View
属性。Silverlight does not contain the concept of a Default view. When you ask a control in Silverlight to bind to a collection it really does bind to the collection, it does not bind to a default view.
As result I don't think there can be a direct and complete port of your extension method. Some re-engineering of your MVVM implementation will be needed. I've not come across the concept of a collection of view model instances before so I'm not exactly sure what would be appropriate in your case.
A couple of approaches I've seen with
CollectionViewSource
is to either have theCollectionViewSource
defined in the Xaml and bind itsSource
to something in the ViewModel. Alternatively have a ViewModel expose aCollectionViewSource
property and have the View xaml bind to itsView
proeprty.您可以做的一件事是手动创建 CollectionViewSource,将其 Source 属性设置为集合,然后使用 CollectionViewSource 的 View 属性获取 CollectionView。
像这样的事情可能会起作用:
One thing that you might be able to do is to manually create the CollectionViewSource, set its Source property to the collection, then get the CollectionView using the View property of the CollectionViewSource.
Something like this might work: