"使用之前的ItemsSource Items集合必须是空的&QUOT。 [英] "Items collection must be empty before using ItemsSource."

查看:836
本文介绍了"使用之前的ItemsSource Items集合必须是空的&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让图像在一个WPF ListView的风格像一个WrapPanel中,因为这老空管Avalon开发团队文章中描述的显示:的How创建自定义视图

I'm trying to get images to display in a WPF ListView styled like a WrapPanel as described in this old ATC Avalon Team article: How to Create a Custom View.

当我尝试填充ADO.NET实体框架对象的LINQ到实体查询收集的ListView我得到以下异常:

When I try to populate the ListView with a LINQ-to-Entities queried collection of ADO.NET Entity Framework objects I get the following exception:

异常

Items集合之前一定要空   使用的ItemsSource。

Items collection must be empty before using ItemsSource.

我的code…

My code…

Visual Basic中

Private Sub Window1_Loaded(...) Handles MyBase.Loaded
    ListViewImages.ItemsSource = From g In db.Graphic _
                                 Order By g.DateAdded Ascending _
                                 Select g
End Sub

XAML

<ListView Name="ListViewImages"
          SelectionMode="Single"
          ItemsSource="{Binding}">
    <local:ImageView />
</ListView>

我把一个断点在该行。 ListViewImages.ItemsSource 没有之前的LINQ分配。

I put a breakpoint on that line. ListViewImages.ItemsSource is Nothing just before the LINQ assignment.

推荐答案

之所以这样特定的异常被抛出的是元素的内容获取应用于ListView的项集合。因此,XAML初始化与一个本地ListView控件:ImageView的在其项集合。但是,使用你必须使用项目属性或ItemsSource属性一个ItemsControl时,你不能同时使用这两种。因此,当在的ItemsSource属性获取处理的异常。

The reason this particular exception gets thrown is that the content of the element gets applied to the ListView's Items collection. So the XAML initialises the ListView with a single local:ImageView in its Items collection. But when using an ItemsControl you must use either the Items property or the ItemsSource property, you can't use both at the same time. Hence when the ItemsSource attribute gets processed an exception is thrown.

您可以找出哪些特性元素的内容将通过查找类上的ContentPropertyAttribute得到应用。在这种情况下,它定义的在类层次结构中较高,在ItemsControl的

You can find out which property the content of an element will get applied to by looking for the ContentPropertyAttribute on the class. In this case it's defined higher in the class hierarchy, on the ItemsControl:

[ContentPropertyAttribute("Items")]

在这里的意图是ListView的视图设置为本地:ImageView的这样的解决方法是明确表示要设置的属性。

The intention here was that the ListView's View be set to a local:ImageView so the fix is to explicitly indicate the property to be set.

修复XAML和异常消失:

Fix the XAML and the exception goes away:

<ListView Name="ListViewImages"
          SelectionMode="Single"
          ItemsSource="{Binding}">
    <ListView.View>
        <local:ImageView />
    </ListView.View>
</ListView>

据缺少&LT; ListView.View&GT; 标记

这篇关于&QUOT;使用之前的ItemsSource Items集合必须是空的&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆