我可以从 xaml 中过滤集合吗?
我有一个 wpf-mvvm 应用程序。
我的视图模型中有一个可观察的集合
public ObservableCollection<BatchImportResultMessageDto> ImportMessageList { get; set; }
“BatchImportResultMessageDto”包含两个属性..
结果类型..和消息。结果类型可以是成功或失败。
我需要在一个列表框中显示成功......并在另一个列表框中显示失败。
我可以通过在视图模型中拥有 2 个可观察集合来保存成功/失败来做到这一点。
public ObservableCollection<BatchImportResultMessageDto> ImportFailureMessageList { get; set; } // To hold the failure messages.
public ObservableCollection<BatchImportResultMessageDto> ImportSuccessMessageList { get; set; } // To hold the sucess messages.
但是还有其他更好的方法可以过滤它(不需要新的两个集合)吗?
I have a wpf-mvvm application.
I have an observable collection in my viewmodel
public ObservableCollection<BatchImportResultMessageDto> ImportMessageList { get; set; }
"BatchImportResultMessageDto" contains two properties..
result type..and message. Result type can be success or failure.
I need to display success in one list box ..and failure in another listbox.
I can do this..by having 2 observable collections in viewmodel to hold success/failure.
public ObservableCollection<BatchImportResultMessageDto> ImportFailureMessageList { get; set; } // To hold the failure messages.
public ObservableCollection<BatchImportResultMessageDto> ImportSuccessMessageList { get; set; } // To hold the sucess messages.
But is there any other better way so that i can filter it (without new two collections) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 CollectionViewSource 并将其设为视图模型的属性,并绑定到该属性,而不是直接从 XAML 绑定到
ImportMessageList
集合。将您的ImportMessageList
集合设置为CollectionViewSource
的源,然后配置谓词以对CollectionViewSource
进行过滤。像这样的东西:
You can use a CollectionViewSource and make it a property of your view model, and bind to that instead of your
ImportMessageList
collection directly from the XAML. Set yourImportMessageList
collection as the Source of theCollectionViewSource
, and then configure a predicate to do your filtering on theCollectionViewSource
.Something like:
您可以通过创建两个 CollectionViewSource 来完成此操作对象并为每个对象设置过滤器。
如何从 VM 绑定在 xaml 中创建 CVS(源):
如何过滤 CVS在后面的代码中(如果您不想引用模型,可以使用反射来查看模型的属性。来源):
with (代码隐藏)
You can do this by creating two CollectionViewSource objects and setting a filter on each.
How to create a CVS in xaml from a VM binding (Source):
How to filter a CVS in a code behind (You can use reflection to look at the properties of your model if you don't want to make a reference to it. Source):
with (code behind)