我有下面的代碼如何在代碼中創建集合查看源代碼背後的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
<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後面創建?
感謝代碼,它解決了這個目的。 – Elangesh 2012-01-13 11:28:05