WPF: "Items collection must be empty before using ItemsSource."

Posted by Zack Peterson on Stack Overflow See other posts from Stack Overflow or by Zack Peterson
Published on 2009-03-25T23:02:11Z Indexed on 2010/03/14 4:25 UTC
Read the original article Hit count: 364

Filed under:
|
|
|
|

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.

WPF ListView WrapPanel Images

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:

Exception

Items collection must be empty before using ItemsSource.

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>

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

© Stack Overflow or respective owner

Related posts about wpf

Related posts about .NET