在使用 itemssource 之前,items 集合必须为空 - datagrid wpf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11814058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
items collection must be empty before using itemssource - datagrid wpf
提问by diamondracer
I keep getting this error when trying to explicitely define the datagrid columns. If I comment out the 3 datagridtext columns, it works (but I want to hide some columns obviously). In my code, all I am doing is setting the datacontext for DataGridLookupsTab..not messing with itemsource or anything in my code. Any idea what I might be doing wrong here ?
尝试明确定义数据网格列时,我不断收到此错误。如果我注释掉 3 个 datagridtext 列,它就可以工作(但我想明显地隐藏一些列)。在我的代码中,我所做的只是为 DataGridLookupsTab 设置数据上下文……不要弄乱 itemsource 或我的代码中的任何内容。知道我在这里可能做错了什么吗?
Thanks !
谢谢 !
<TabItem Header="Lookups" Height="23" VerticalAlignment="Top">
<Grid x:Name="DataGridLookupsTab" Background="#FFE5E5E5" Margin="0,0,-1,-2" DataContext="Apps.OMS.Models.Lookups">
<ComboBox x:Name="ComboBoxDistinctLookups" HorizontalAlignment="Left" Margin="128,14,0,0" VerticalAlignment="Top" Width="228" ItemsSource="{Binding Path=LookupTypeList}" SelectionChanged="ComboBoxDistinctLookups_SelectionChanged"/>
<Label Content="Select Lookup Type" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.105,4.731"/>
<DataGrid x:Name="DataGridLookups" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" Width="768" Height="327" ItemsSource="{Binding LookupRecords}" >
<DataGridTextColumn Binding="{Binding LookupType}" Header="Lookup Type" />
<DataGridTextColumn Binding="{Binding LookupValue}" Header="Lookup Value" />
<DataGridTextColumn Binding="{Binding LookupDescription}" Header="Lookup Description" />
</DataGrid>
</Grid>
</TabItem>
回答by diamondracer
Actually the problem ended up being my lack of the Datagrid.Columns tag. Thanks
实际上,问题最终是我缺少 Datagrid.Columns 标记。谢谢
回答by Phil
If you're setting the DataContext in your code (meaning your background code file), then DON'T set your datacontext in your XAML. Instead, use ItemSource={Binding }. See if that fixes your issue.
如果您在代码中设置 DataContext(即后台代码文件),则不要在 XAML 中设置 datacontext。相反,使用 ItemSource={Binding }。看看这是否能解决您的问题。

