在ViewModel中使用CollectionViewSource的正确方法

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

我使用拖放将数据源对象(DB模型)绑定到 DataGrid (基本上遵循此示例 msdn.microsoft/en-us/data/jj574514 。一切都可以正常执行。

I used Drag and Drop to bind Data Source object (a DB model) to DataGrid (basically following this example msdn.microsoft/en-us/data/jj574514. Everything works fine with this implementation.

XAML:

<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; }

之后,我重构了应用程序并创建了一个ViewModel:

After that I refactor the application and create a ViewModel:

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

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

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).

我真的很感激任何帮助,指出我正确的方法实现 CollectionViewSource 和 DataBinding 与 DataGrid 。

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 -

  • 通过您的<$(类别)在您的案例中暴露 ObserVableCollection c $ c> ViewModel 并在xaml中创建 CollectionViewSource ,这样 -

  • 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>

    看到 - 使用CollectionViewSource从XAML中过滤收集

    直接从您的 ViewModel

    创建并公开一个 ICollectionView 请参阅 - 如何在WPF中浏览,分组,排序和过滤数据

    see this - How to Navigate, Group, Sort and Filter Data in 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"> <ListBox ItemsSource={Binding Customers} /> </Window>

    查看Codebehind:

    View Codebehind:

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

    ViewModel: / p>

    ViewModel:

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

  • 更多推荐

    在ViewModel中使用CollectionViewSource的正确方法

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

    发布评论

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

    >www.elefans.com

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