如何在 wpf 应用程序的代码隐藏中创建集合视图源
我有以下代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CollectionViewSource.GetDefaultView(depts)
返回一个ICollectionView
。CollectionViewSource
主要是一种根据提供的集合确定要使用的ICollectionView
类型的方法。但是,如果您确实想创建一个
CollectionViewSource
,您可能可以这样做:但是我确实相信您想要实现的目标可以通过更好的方式来完成。例如:
CollectionViewSource.GetDefaultView(depts)
returns anICollectionView
.CollectionViewSource
is mainly a means to determine what type ofICollectionView
to use depending upon the collection provided.If you really do want to create a
CollectionViewSource
however, you could probably do it like so:I do however believe what you are trying to achieve could be done in a better way. For example:
CollectionViewSource.GetDefaultView 方法返回一个 ICollectionView
但如果您绑定到一个继承自 IList 的集合(在您的情况下就是如此),它也可以转换为更强大的类型...
这就是编译器想要做的,但不能。因此出现了错误。因此,将您的“cvs”更改为适当的类型...
ICollectionView 或 ListCollectionView... 取决于您想要对结果执行的操作...
注意:Bea Stolnitz 关于绑定到 CollectionView 的开创性博客文章位于 她的旧博客
The CollectionViewSource.GetDefaultView method returns an ICollectionView
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...
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