【问题标题】:items collection must be empty before using itemssource - datagrid wpf在使用 itemssource - datagrid wpf 之前,项目集合必须为空
【发布时间】:2012-08-02 14:01:44
【问题描述】:

尝试显式定义数据网格列时,我不断收到此错误。如果我注释掉 3 个 datagridtext 列,它可以工作(但我显然想隐藏一些列)。在我的代码中,我所做的只是为 DataGridLookupsTab 设置数据上下文。不要弄乱 itemsource 或我的代码中的任何内容。知道我在这里可能做错了什么吗?

谢谢!

<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>

【问题讨论】:

标签: c# wpf datagrid


【解决方案1】:

如果您在代码中设置 DataContext(即后台代码文件),则不要在 XAML 中设置数据上下文。相反,请使用 ItemSource={Binding }。看看这是否能解决您的问题。

【讨论】:

    【解决方案2】:

    实际上问题最终是我缺少 Datagrid.Columns 标记。 谢谢

    【讨论】:

      猜你喜欢
      • 2014-01-07
      • 1970-01-01
      • 2012-07-24
      • 2010-10-15
      • 1970-01-01
      • 2012-03-04
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多