我通常更喜欢在视图模型中公开一个集合并在 XAML 中创建集合视图源:
<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}}" />
Run Code Online (Sandbox Code Playgroud)
和行为类:
public class MyFilterLogic: Behavior<CollectionViewSource>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Filter += AssociatedObjectOnFilter;
}
private void AssociatedObjectOnFilter(object sender, FilterEventArgs filterEventArgs)
{
// filter logic
}
}
Run Code Online (Sandbox Code Playgroud)
其他一些专家实际上不介意从他们的视图模型中公开 CollectionView:https : //stackoverflow.com/a/979943/3351315
| 归档时间: |
|
| 查看次数: |
1539 次 |
| 最近记录: |