“使用ItemsSource之前,项目集合必须是空的。”
我试图让图像显示在一个WPF ListView风格像一个WrapPanel样式在这个旧的ATC Avalon团队文章: 如何创build一个自定义视图中描述 。

当我尝试用ADO.NETentity framework对象的LINQ-to-Entities查询集合填充ListView时,我得到以下exception:
例外
在使用ItemsSource之前,项目集合必须是空的。
我的代码…
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分配之前是Nothing 。 
这个特殊的exception被抛出的原因是元素的内容被应用到ListView的Items集合中。 所以XAML使用一个本地的ImageView在其Items集合中初始化ListView。 但是在使用ItemsControl时,必须使用Items属性或ItemsSource属性,但不能同时使用两者。 因此,在处理ItemsSource属性时会引发exception。
您可以通过在类上查找ContentPropertyAttribute来找出元素的内容将应用于哪个属性。 在这种情况下,它在ItemsControl中的类层次结构中定义得更高:
 [ContentPropertyAttribute("Items")] 
这里的意图是ListView的视图被设置为本地:ImageView,所以修复是明确指出要设置的属性。
修复XAML,exception消失:
 <ListView Name="ListViewImages" SelectionMode="Single" ItemsSource="{Binding}"> <ListView.View> <local:ImageView /> </ListView.View> </ListView> 
 它缺less<ListView.View>标记。 
我有一个稍微不同的情况下,同样的错误一段时间。 我有
 <wpftoolkit:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=Accounts}" > <wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" /> </wpftoolkit:DataGrid> 
我固定的是
 <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> 
我刚碰到这个问题的一个非常阴险的例子。 我原来的片段要复杂得多,很难看清错误。
  <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> 
 错误? 最初打开<ItemsControl>标签之后的额外> !  <被应用于内置的Items集合。 当DataContext被设置后,即时crashola。 因此,在debugging此问题时,请注意不仅仅是围绕您的ItemsControl特定数据子项的错误。 
我也是一个不同的场景。
 <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> 
现在当你完成丢失的标签Control.ItemTemplate ,一切都正常:
 <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> 
我在不同的情况下有这个相同的错误
 <ItemsControl ItemsSource="{Binding TableList}"> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl> 
 解决方法是在ItemsControl.ItemsPanel之前添加ItemsControl.ItemsPanel标签 
 <ItemsControl ItemsSource="{Binding TableList}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 
对于那些阅读本页的人这不是一个例子 ,这是这个例外背后的一般原因 ,我通过总结答案节省时间:
确保你没有不小心添加额外的标签,或错过了一个标签。
 您不能通过不同的方法(Binding / direct Source)将项目添加到ItemsSource 。 所以你应该检查的第一件事是在ItemsControl(或其他类似的控件)应该只是它自己的标签: 
 <!--Right--> <ItemsControl ItemsSource="{Binding MyItems}"> <ItemsControl.ItemsPanel.../> <ItemsControl.ItemContainerStyle.../> <ItemsControl.Width.../> </ItemsControl> <!--WRONG--> <ItemsControl ItemsSource="{Binding MyItems}"> <Grid.../> <Button.../> <DataTemplate.../> </ItemsControl> 
 原因是ItemsControl源码已经通过Binding设置,所以其他项目(Grid,Button,…)不能被添加。 但是,如果ItemsSource不用于绑定它可以这样做: 
 <!--Right--> <ItemsControl> <Button.../> <TextBlock.../> </ItemsControl> 
用不同的方式表述答案; 确认没有丢失父节点或不正确的节点 。
这个失败:
  ItemsPanelTemplate子节点没有父节点(或错误的父节点) 
 <ItemsControl ItemsSource="{Binding TimeSpanChoices}"> <ItemsPanelTemplate> <UniformGrid Rows="1" /> </ItemsPanelTemplate> ... </ItemsControl> 
这工作:
 <ItemsControl ItemsSource="{Binding TimeSpanChoices}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="1" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> ... </ItemsControl> 
在我的情况下,它只是一个额外的ListPanel内的StackPanel:
 <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> 
变为:
 <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> 
一切都很好。
保持DataGrid.Columns中的模板列。 这帮助我解决了这个问题。
Ref: DataGridTemplateColumn:在使用ItemsSource之前,Items集合必须是空的。
在我的情况下,它不是使用ItemsControl的DataTemplate。
旧:
 <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> 
新:
 <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> 
 当我尝试应用上下文菜单到我的TreeView时,我有这个错误。 那些尝试结束了一个不好的XAML编译: 
 <TreeView Height="Auto" MinHeight="100" ItemsSource="{Binding Path=TreeNodes, Mode=TwoWay}" ContextMenu="{Binding Converter={StaticResource ContextMenuConverter}}"> ContextMenu=""> <TreeView.ItemContainerStyle> ... 
 注意有问题的一行: ContextMenu=""> 。 
 我不知道它为什么编译,但我认为值得一提的是这个神秘的例外信息的原因。 就像Armentage说的那样,仔细观察XAML,特别是在你最近编辑的地方。 
 我的是一个datagrid风格。 如果你忽略了样式周围的<DataGrid.RowStyle>标签,你会遇到这个问题。 奇怪的是,它是这样的一段时间的工作。 这是错误的代码。 
  <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> 
和好
  <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> 
我有同样的错误。 问题是在标签</ComboBox.SelectedValue>和</ ComboBox>之间错误地添加了这个额外的符号“>”:
 <ComboBox ItemsSource="{Binding StatusTypes}" DisplayMemberPath="StatusName" SelectedValuePath="StatusID"> <ComboBox.SelectedValue> <Binding Path="StatusID"/> </ComboBox.SelectedValue> > </ComboBox> 
这里是正确的代码:
 <ComboBox ItemsSource="{Binding StatusTypes}" DisplayMemberPath="StatusName" SelectedValuePath="StatusID"> <ComboBox.SelectedValue> <Binding Path="StatusID"/> </ComboBox.SelectedValue> </ComboBox> 
 也许不是这样一个有用的答案,但是在更改列顺序时出现同样的问题,并且在下面的示例中出现了错误。 有很多列,我重新sorting他们,并在closures标记/ /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> 
无论如何,因此而失去了半个小时。 希望这可以帮助别人。
谨防错别字! 我有以下
 <TreeView ItemsSource="{Binding MyCollection}"> <TreeView.Resources> ... </TreeView.Resouces>> </TreeView> 
(注意拖尾> ,这被解释为内容,所以你设置了两倍的内容…花了我一会儿:)