WPF: “Items collection must be empty before using 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.

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. 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") 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.

Fix the XAML and the exception goes away: It was missing that tag.

I had this same error for a while in a slightly different scenario. I had which I fixed to be.

1 Wow, that solved my problem, thanks a lot. – Serious Oct 19 '09 at 9:27 1 +1 from me. Solves my problem too :-) – Gabriel Magana Nov 24 '09 at 22:45 1 Thank you!

Such a simple problem... but such a confusing error. – Scott Feb 2 '10 at 20:54.

Me too on a different scenario. Now when you complete with the missing tag Control. ItemTemplate, everything gets to normal.

I just ran into a VERY incideous example of this problem. My original fragment was much more complex, which made it difficult to see the error. > The bug?

The extra > after the initial opening tag! The So look out for more than just errors surround your ItemsControl specific data children when debugging this problem.

The same happened to me: Extra > => Exception – surfen Dec 11 at 1:51.

In My case, it was just an extra StackPanel inside the ListView: Becomes: and all is well.

I had this same error in a different scenario the solution was to add the "ItemsControl. ItemsPanel" tag befor the "ItemsPanelTemplate.

Wow! You made my day! – sergiol May 9 at 11:05.

I've had this error when I tried applying context menus to my TreeView. Those tries ended up in a bad XAML which compiled somehow: ContextMenu=""> ... Note the problematic line: ContextMenu=""> . I don't know why it compiled, but I figured it's worth mentioning as a reason for this cryptic exception message.

Like Armentage said, look around the XAML carefully, especially in places you've recently edited.

Perhaps not such an useful answer, but I had the same problem when changing column order and made mistake like the one in the following sample. Having a lot of columns, I reordered them and somehow pasted one after closing tag /DataGrid. Columns: Anyway, lost half an hour because of this.

Hope this helps others.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions