CollectionViewSource.GetDefaultView 不在 Silverlight 3 中!解决方法是什么?

发布于 2024-08-26 10:00:15 字数 605 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-09-02 10:00:15

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 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.

等待我真够勒 2024-09-02 10:00:15

您可以做的一件事是手动创建 CollectionViewSource,将其 Source 属性设置为集合,然后使用 CollectionViewSource 的 View 属性获取 CollectionView。

像这样的事情可能会起作用:

public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel,
    ViewModelType collectionItem,
    ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase
{
    Debug.Assert(collection.Contains(collectionItem));
    CollectionViewSource collectionViewSource = new CollectionViewSource();
    collectionViewSource.Source = collection;
    ICollectionView collectionView = collectionViewSource.View;
    if(collectionView != null) collectionView.MoveCurrentTo(collectionItem);
}

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:

public static void SetActiveViewModel<ViewModelType>(this ViewModelBase viewModel,
    ViewModelType collectionItem,
    ObservableCollection<ViewModelType> collection) where ViewModelType : ViewModelBase
{
    Debug.Assert(collection.Contains(collectionItem));
    CollectionViewSource collectionViewSource = new CollectionViewSource();
    collectionViewSource.Source = collection;
    ICollectionView collectionView = collectionViewSource.View;
    if(collectionView != null) collectionView.MoveCurrentTo(collectionItem);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文