问题描述
Silverlight 3中的 CollectionViewSource.GetDefaultView()
方法不是 。在WPF中,我有这个扩展方法:
public static void SetActiveViewModel< ViewModelType>(这个ViewModelBase viewModel,
ViewModelType collectionItem,
ObservableCollection< ViewModelType>集合)其中ViewModelType: ViewModelBase
{
Debug.Assert(collection.Contains(collectionItem));
ICollectionView collectionView = CollectionViewSource.GetDefaultView(collection);
if(collectionView!= null)collectionView.MoveCurrentTo(collectionItem);
}
如何用Silverlight 3编写?
Silverlight不包含默认视图的概念。当你要求一个控件在Silverlight绑定到一个集合,它确实绑定到集合,它不绑定到默认视图。
结果我不认为可以有一个直接和完整的端口你的扩展方法。将需要对您的MVVM实现进行一些重新设计。我没有遇到视图模型实例集合的概念,所以我不能确定什么是适当的你的情况。
一些方法我看到 CollectionViewSource
是要么在Xaml中定义 CollectionViewSource
并绑定其<$ c $ c > Source 到ViewModel中的内容。或者有一个ViewModel公开一个 CollectionViewSource
属性,并使View xaml绑定到它的 View
proeprty。
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?
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 the CollectionViewSource
defined in the Xaml and bind its Source
to something in the ViewModel. Alternatively have a ViewModel expose a CollectionViewSource
property and have the View xaml bind to its View
proeprty.
这篇关于CollectionViewSource.GetDefaultView不在Silverlight 3!有什么解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!