MVVM:CollectionView在ViewModel或CollectionViewSource在xaml? [英] MVVM: CollectionView in ViewModel or CollectionViewSource in xaml?

查看:97
本文介绍了MVVM:CollectionView在ViewModel或CollectionViewSource在xaml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVVM模式开发一个WPF应用程序,我需要在ListView中显示项目列表(带过滤),所选项目的字段显示在主/详细视图中。我在以下两种方式之间撕裂:

I'm developing a WPF application using the MVVM pattern and I need to display a list of items in a ListView (with filtering), with the fields of the selected item displayed in a Master/Detail view. I'm torn between the following two ways of doing this:


  1. 在我的ViewModel中显示一个CollectionView,并绑定到此。

  2. 在我的ViewModel中公开一个纯IList,并使用CollectionViewSource在XAML中创建CollectionView。

有一个接受的方法这样做?

Is there an accepted way of doing this? Any thoughts on the best way?

推荐答案

我做了前者(暴露 CollectionView 从VM),但真正重要的是你的过滤逻辑是。这肯定是在VM中,以便它可以测试,即使视图必须将 CollectionViewSource 连接到VM。也就是说,我不认为有什么特别讨厌或反MVVM暴露一个CollectionView从您的VM。

I do the former (expose CollectionView from the VM) but what really matters is where your filtering logic is. That should definitely be in the VM so that it can be tested, even if the view has to wire up the CollectionViewSource to the VM. That said, I don't think there's anything particularly nasty or anti-MVVM about exposing a CollectionView from your VM.

这篇关于MVVM:CollectionView在ViewModel或CollectionViewSource在xaml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆