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.
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.
This question is related to
.net
wpf
vb.net
entity-framework
binding
?? To state the answer differently ??
In Xaml verify that there are no Missing Parent Nodes or incorrect nodes in the defined areas.
For example
There is no proper parent for the ItemsPanelTemplate
child node below:
<ItemsControl ItemsSource="{Binding TimeSpanChoices}">
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
...
</ItemsControl>
<ItemsControl ItemsSource="{Binding TimeSpanChoices}">
<ItemsControl.ItemsPanel> <!-- I am the missing parent! -->
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
...
</ItemsControl>
There is a proper parent node of <ItemsControl.ItemsPanel>
provided^^^.
I had the same error. The problem was this extra symbol ">" added by mistake between the tags </ComboBox.SelectedValue> and </ComboBox>:
<ComboBox
ItemsSource="{Binding StatusTypes}"
DisplayMemberPath="StatusName"
SelectedValuePath="StatusID">
<ComboBox.SelectedValue>
<Binding Path="StatusID"/>
</ComboBox.SelectedValue>
>
</ComboBox>
and here is the correct code:
<ComboBox
ItemsSource="{Binding StatusTypes}"
DisplayMemberPath="StatusName"
SelectedValuePath="StatusID">
<ComboBox.SelectedValue>
<Binding Path="StatusID"/>
</ComboBox.SelectedValue>
</ComboBox>
In my case, it was not using a DataTemplate for the ItemsControl.
Old:
<ItemsControl Width="243" ItemsSource="{Binding List, Mode=TwoWay}">
<StackPanel Orientation="Horizontal">
<TextBox Width="25" Margin="0,0,5,0" Text="{Binding Path=Property1}"/>
<Label Content="{Binding Path=Property2}"/>
</StackPanel>
</ItemsControl>
New:
<ItemsControl Width="243" ItemsSource="{Binding List, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="25" Margin="0,0,5,0" Text="{Binding Path=Property1}"/>
<Label Content="{Binding Path=Property2}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I ran into this problem because one level of tag, <ListView.View>
to be specirfic, was missing in my XAML.
This code produced this error.
<Grid>
<ListView Margin="10" Name="lvDataBinding" >
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView>
</Grid>
The following fixed it
<Grid>
<ListView Margin="10" Name="lvDataBinding" >
<ListView.View> <!-- This was missing in top! -->
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
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
:
<DataGridTemplateColumn x:Name="addedDateColumn" Header="Added Date" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=AddedDate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGridTemplateColumn x:Name="rowguidColumn" Header="rowguid" Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=rowguid}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid>
Anyway, lost half an hour because of this. Hope this helps others.
Beware of typos! I had the following
<TreeView ItemsSource="{Binding MyCollection}">
<TreeView.Resources>
...
</TreeView.Resouces>>
</TreeView>
(Notice the tailing >, which is interpreted as content, so you're setting twice the content... Took me a while :)
I just ran into a VERY insidious example of this problem. My original fragment was much more complex, which made it difficult to see the error.
<ItemsControl
Foreground="Black" Background="White" Grid.IsSharedSizingScope="True"
x:Name="MyGrid" ItemsSource="{Binding}">
>
<ItemsControl.ItemsPanel>
<!-- All is fine here -->
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<!-- All is fine here -->
</ItemsControl.ItemTemplate>
<!-- Have you caught the error yet? -->
</ItemsControl>
The bug? The extra > after the initial opening <ItemsControl>
tag! The <
got applied to the built-in Items collection. When the DataContext was later set, instant crashola. So look out for more than just errors surround your ItemsControl specific data children when debugging this problem.
Exception
Items collection must be empty before using ItemsSource.
This exception occurs when you add items to the ItemsSource
through different sources. So
Make sure you haven't accidentally missed a tag, misplaced a tag, added extra tags, or miswrote a tag.
<!--Right-->
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemsPanel.../>
<ItemsControl.MyAttachedProperty.../>
<FrameworkElement.ActualWidth.../>
</ItemsControl>
<!--WRONG-->
<ItemsControl ItemsSource="{Binding MyItems}">
<Grid.../>
<Button.../>
<DataTemplate.../>
<Heigth.../>
</ItemsControl>
While ItemsControl.ItemsSource
is already set through Binding
, other items (Grid, Button, ...) can't be added to the source.
However while ItemsSource
is not in-use the following code is allowed:
<!--Right-->
<ItemsControl>
<Button.../>
<TextBlock.../>
<sys:String.../>
</ItemsControl>
notice the missing ItemsSource="{Binding MyItems}"
part.
Keep template column inside DataGrid.Columns. This helped me resolve this issue.
Ref: DataGridTemplateColumn : Items collection must be empty before using ItemsSource.
Mine was with a datagrid Style. If you leave out the <DataGrid.RowStyle>
tags around the Style you get that problem. Weird thing is it worked for a while like that. Here is the bad code.
<DataGrid Name="DicsountScheduleItemsDataGrid"
Grid.Column="0"
Grid.Row="2"
AutoGenerateColumns="false"
ItemsSource="{Binding DiscountScheduleItems, Mode=OneWay}">
<Style TargetType="DataGridRow">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
and the good
<DataGrid Name="DicsountScheduleItemsDataGrid"
Grid.Column="0"
Grid.Row="2"
AutoGenerateColumns="false"
ItemsSource="{Binding DiscountScheduleItems, Mode=OneWay}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</DataGrid.RowStyle>
I had this same error in a different scenario
<ItemsControl ItemsSource="{Binding TableList}">
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl>
The solution was to add the ItemsControl.ItemsPanel
tag before the ItemsPanelTemplate
<ItemsControl ItemsSource="{Binding TableList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
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:
<TreeView Height="Auto" MinHeight="100" ItemsSource="{Binding Path=TreeNodes, Mode=TwoWay}"
ContextMenu="{Binding Converter={StaticResource ContextMenuConverter}}">
ContextMenu="">
<TreeView.ItemContainerStyle>
...
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.
I encountered this error in another situation. I tried to defined a style for TreeViewItems directly within the <TreeView>
, but instead should have embedded that within <TreeView.ItemContainerStyle>
.
Wrong:
<TreeView ItemsSource="{Binding ExampleListView}">
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubItemListList}">
...
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Correct:
<TreeView ItemsSource="{Binding ExampleListView}">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubItemListList}">
...
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Me too on a different scenario.
<ComboBox Cursor="Hand" DataContext="{Binding}"
FontSize="16" Height="27" ItemsSource="{Binding}"
Name="cbxDamnCombo" SelectedIndex="0" SelectedValuePath="MemberId">
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="MemberName"/>
<Binding Path="Phone"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox>
Now when you complete with the missing tag Control.ItemTemplate, everything gets to normal:
<ComboBox Cursor="Hand" DataContext="{Binding}"
FontSize="16" Height="27" ItemsSource="{Binding}"
Name="cbxDamnCombo" SelectedIndex="0" SelectedValuePath="MemberId">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} / {1}">
<Binding Path="MemberName"/>
<Binding Path="Phone"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
<ComboBox.ItemTemplate>
</ComboBox>
I had this same error for a while in a slightly different scenario. I had
<wpftoolkit:DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}" >
<wpftoolkit:DataGridTextColumn
Header="Account Name"
Binding="{Binding Path=AccountName}" />
</wpftoolkit:DataGrid>
which I fixed to be
<wpftoolkit:DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}" >
<wpftoolkit:DataGrid.Columns>
<wpftoolkit:DataGridTextColumn
Header="Account Name"
Binding="{Binding Path=AccountName}" />
</wpftoolkit:DataGrid.Columns>
</wpftoolkit:DataGrid>
In My case, it was just an extra StackPanel inside the ListView:
<ListView Name="_details" Margin="50,0,50,0">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Location.LicenseName, StringFormat='Location: {0}'}"/>
<TextBlock Text="{Binding Ticket.Employee.s_name, StringFormat='Served by: {0}'}"/>
<TextBlock Text="{Binding Ticket.dt_create_time, StringFormat='Started at: {0}'}"/>
<Line StrokeThickness="2" Stroke="Gray" Stretch="Fill" Margin="0,5,0,5" />
<ItemsControl ItemsSource="{Binding Items}"/>
</StackPanel>
</StackPanel>
</ListView>
Becomes:
<ListView Name="_details" Margin="50,0,50,0">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Location.LicenseName, StringFormat='Location: {0}'}"/>
<TextBlock Text="{Binding Ticket.Employee.s_name, StringFormat='Served by: {0}'}"/>
<TextBlock Text="{Binding Ticket.dt_create_time, StringFormat='Started at: {0}'}"/>
<Line StrokeThickness="2" Stroke="Gray" Stretch="Fill" Margin="0,5,0,5" />
<ItemsControl ItemsSource="{Binding Items}"/>
</StackPanel>
</ListView>
and all is well.
Source: Stackoverflow.com