English 中文(简体)
• 如何在轮用背后的法典中建立收集源
原标题:How to create collection view source in code behind for wpf app

我遵循法典

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;

However while executing following line of code

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

它是个例外,唯一的例外是:

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

有些人可以帮助我,提供如何创建收集资料。 采用背后代码的观点?

最佳回答

<代码>CollectionViewSource.GetDefaultView(depts) 返回 意见。 意见,视所提供的收集情况而定。

如果你真的想建立<条码> ,可浏览条码<>。 然而,你可能这样做:

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

然而,我确实认为,你想要实现的目标可以做得更好。 例如:

var collectionViewSource = this.FindResource("Departments") as CollectionViewSource;
collectionViewSource.Source = depts;
问题回答

收听器法 观点

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

但是,如果你对来自国际男女同性恋者协会的继承书具有约束力(在你看来是这样),也可以归入一个更强大的类别。

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

汇编者想要做什么,但不能做。 因此错误。 因此,将你的遗体改为适当的类别......

ICollection 观点或清单 观点......取决于你想要与结果做些什么......

NOTE: Bea Stolnitz s tnal blog entry on binding to a CollectionView is at her古老博客





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...