在 ViewModel 中使用 CollectionViewSource 的正确方法 [英] Proper way to use CollectionViewSource in ViewModel

查看:19
本文介绍了在 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 实现 CollectionViewSourceDataBinding 的正确方法.

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 -

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

  1. 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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/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 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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