如何在 wpf 应用程序的代码隐藏中创建集合视图源

发布于 2024-12-26 08:01:07 字数 1084 浏览 1 评论 0原文

我有以下代码

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var entities  = new DemoEntities();
            var depts = entities.Depts.ToList(); // entity framwork dept table
            CollectionViewSource cvs = (CollectionViewSource)CollectionViewSource.GetDefaultView(depts);
        }
    }

我的目的是将此集合绑定到 XAML 中的以下 Windows 资源

<Window.Resources>
        <CollectionViewSource x:Key="Departments"/>
    </Window.Resources>

,但是在执行

CollectionViewSource collectionViewSource = this.FindResource("Departments") as CollectionViewSource;

以下代码行时

CollectionViewSource cvs = (CollectionViewSource)CollectionViewSource.GetDefaultView(depts);

它抛出一个异常,并且该异常的内部异常如下

{"Unable to cast object of type 'System.Windows.Data.ListCollectionView' to type 'System.Windows.Data.CollectionViewSource'."}

有人可以通过提供如何使用隐藏代码创建 CollectionViewSource 来帮助我吗?

I have following code

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var entities  = new DemoEntities();
            var depts = entities.Depts.ToList(); // entity framwork dept table
            CollectionViewSource cvs = (CollectionViewSource)CollectionViewSource.GetDefaultView(depts);
        }
    }

My intention is to bind this collection the following windows resource in XAML

<Window.Resources>
        <CollectionViewSource x:Key="Departments"/>
    </Window.Resources>

Using

CollectionViewSource collectionViewSource = this.FindResource("Departments") as CollectionViewSource;

However while executing following line of code

CollectionViewSource cvs = (CollectionViewSource)CollectionViewSource.GetDefaultView(depts);

it is throwing an exception and that exception's inner exception is following

{"Unable to cast object of type 'System.Windows.Data.ListCollectionView' to type 'System.Windows.Data.CollectionViewSource'."}

Could some one help me on this by providing how to create CollectionViewSource using code behind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2025-01-02 08:01:07

CollectionViewSource.GetDefaultView(depts) 返回一个 ICollectionViewCollectionViewSource 主要是一种根据提供的集合确定要使用的 ICollectionView 类型的方法。

但是,如果您确实想创建一个CollectionViewSource,您可能可以这样做:

var collectionViewSource = new CollectionViewSource();
collectionViewSource.Source = depts;

但是我确实相信您想要实现的目标可以通过更好的方式来完成。例如:

var collectionViewSource = this.FindResource("Departments") as CollectionViewSource;
collectionViewSource.Source = depts;

CollectionViewSource.GetDefaultView(depts) returns an ICollectionView. CollectionViewSource is mainly a means to determine what type of ICollectionView to use depending upon the collection provided.

If you really do want to create a CollectionViewSource however, you could probably do it like so:

var collectionViewSource = new CollectionViewSource();
collectionViewSource.Source = depts;

I do however believe what you are trying to achieve could be done in a better way. For example:

var collectionViewSource = this.FindResource("Departments") as CollectionViewSource;
collectionViewSource.Source = depts;
夜无邪 2025-01-02 08:01:07

CollectionViewSource.GetDefaultView 方法返回一个 ICollectionView

ICollectionView icv = CollectionViewSource.GetDefaultView(dg1.ItemsSource);

但如果您绑定到一个继承自 IList 的集合(在您的情况下就是如此),它也可以转换为更强大的类型...

ListCollectionView icv = CollectionViewSource.GetDefaultView(dg1.ItemsSource) 
                as ListCollectionView;

这就是编译器想要做的,但不能。因此出现了错误。因此,将您的“cvs”更改为适当的类型...

ICollectionView 或 ListCollectionView... 取决于您想要对结果执行的操作...

注意:Bea Stolnitz 关于绑定到 CollectionView 的开创性博客文章位于 她的旧博客

The CollectionViewSource.GetDefaultView method returns an ICollectionView

ICollectionView icv = CollectionViewSource.GetDefaultView(dg1.ItemsSource);

But if you are binding to a collection that inherits from IList (which it does in your case), it can also be cast to a more powerful type...

ListCollectionView icv = CollectionViewSource.GetDefaultView(dg1.ItemsSource) 
                as ListCollectionView;

Which is what the compiler wants to do, but cannot. Hence the error. So change your 'cvs' to be of the appropriate type...

ICollectionView or ListCollectionView... depending upon what you want to do with the result...

NOTE: Bea Stolnitz's seminal blog entry on binding to a CollectionView is at her old blog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文