gpt4 book ai didi

c# - CollectionViewSource 违反 MVVM

转载 作者:太空狗 更新时间:2023-10-30 01:04:17 31 4
gpt4 key购买 nike

我有一个 MVVM 应用程序,在我的几个 VM 中,我使用 CollectionViewSource.GetDefaultView(datasource) 来初始化我的 ICollectionView,它运行良好。我担心的是,在我的 VM 中使用 CVS 时是否违反了 MVVM?

谢谢大家的意见

最佳答案

我通常更喜欢在 View 模型中公开集合并在 XAML 中创建 Collection View 源:

<Window.Resources>
<CollectionViewSource x:Key="CollectionViewSource" Source="{Binding Items}">
<i:Interaction.Behaviors>
<behaviors:MyFilterLogic />
</i:Interaction.Behaviors>
</CollectionViewSource>
</Window.Resources>

<ItemsControl ItemsSource="{Binding Source={StaticResource CollectionViewSource}}" />

以及行为类:

public class MyFilterLogic: Behavior<CollectionViewSource>
{
protected override void OnAttached()
{
base.OnAttached();

AssociatedObject.Filter += AssociatedObjectOnFilter;
}

private void AssociatedObjectOnFilter(object sender, FilterEventArgs filterEventArgs)
{
// filter logic
}
}

其他一些专家实际上并不介意从他们的 View 模型中公开 CollectionView: https://stackoverflow.com/a/979943/3351315

关于c# - CollectionViewSource 违反 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24142185/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com