在 ViewModel 中使用 CollectionViewSource 的正确方法

编程入门 行业动态 更新时间:2024-10-11 15:14:02
本文介绍了在 ViewModel 中使用 CollectionViewSource 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用拖放将数据源对象(一个数据库模型)绑定到 DataGrid(基本上遵循 实体框架数据绑定与 WPF.

I used Drag and Drop to bind Data Source object (a DB model) to DataGrid (basically following this example in Entity Framework Databinding with WPF.

这个实现一切正常.

<Window.Resources> <CollectionViewSource x:Key="categoryViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Category}, CreateList=True}"/> </Window.Resources> <Grid DataContext="{StaticResource categoryViewSource}"> ..

背后的代码

private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Data.CollectionViewSource categoryViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("categoryViewSource"))); _context.Categories.Load(); categoryViewSource.Source = _context.Categories.Local; }

视图模型

public MainWindow() { InitializeComponent(); this.DataContext = new MyViewModel(); }

但是,当我尝试在 ViewModel 中使用相同的代码时,它不起作用(FindResource 不可用),此外,我认为这不是正确的方法(即在 MVVM 中使用 x:Key).

However, when I try to use the same code from within ViewModel, it doesn‘t work (FindResource is not available), besides, I don’t think this is the right approach (i.e. to use x:Key in MVVM).

我真的很感谢任何帮助指出什么是使用 DataGrid 实现 CollectionViewSource 和 DataBinding 的正确方法.

I would really appreciate any help to point me what is the right way to implement CollectionViewSource and DataBinding with DataGrid.

推荐答案

您有两个选项可以在 MVVM 中正确使用 CollectionViewSource -

You have two options to use CollectionViewSource properly with MVVM -

  • 通过您的 ViewModel 公开项目的 ObservableCollection(在您的情况下为 Categories)并创建 CollectionViewSource 在像这样的 XAML 中 -

  • Expose an ObservableCollection of items (Categories in your case) through your ViewModel and create CollectionViewSource in XAML like this - <CollectionViewSource Source="{Binding Path=Categories}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="CategoryName" /> </CollectionViewSource.SortDescriptions> </CollectionViewSource>

    scm:xmlns:scm="clr-namespace:System.ComponentModel;assembly=Wind‌ owsBase"

    看到这个 - Filtering 使用 CollectionViewSource 的 XAML 集合

    see this - Filtering collections from XAML using CollectionViewSource

    直接从您的 ViewModel

    看到这个 - 如何在 WPF 中导航、分组、排序和过滤数据

    以下示例显示了如何创建集合视图和将其绑定到 ListBox

    Following example shows how to create a collection view and bind it to a ListBox

    查看 XAML:

    <Window xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" x:Class="CustomerView"> <ListBox ItemsSource={Binding Customers} /> </Window>

    查看代码隐藏:

    public class CustomerView : Window { public CustomerView() { DataContext = new CustomerViewModel(); } }

    视图模型:

    public class CustomerViewModel { private readonly ICollectionView customerView; public ICollectionView Customers { get { return customerView; } } public CustomerViewModel() { IList<Customer> customers = GetCustomers(); customerView = CollectionViewSource.GetDefaultView( customers ); } }

    更新:

    问.如果没有可排序的属性?例如如果有字符串或整数的 ObservableCollection?

    Q. If there is no property to sort on? e.g. if there is an ObservableCollection of string or int?

    A.在这种情况下,您可以简单地使用 . 作为属性名称:

    A. In that case you can Simply use . as the property name:

    <scm:SortDescription PropertyName="." />
  • 更多推荐

    在 ViewModel 中使用 CollectionViewSource 的正确方法

    本文发布于:2023-10-11 17:29:29,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1482354.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:正确   方法   ViewModel   CollectionViewSource

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!