diamondracer
diamondracer

Reputation: 95

items collection must be empty before using itemssource - datagrid wpf

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 ?

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>

Upvotes: 2

Views: 6348

Answers (2)

diamondracer
diamondracer

Reputation: 95

Actually the problem ended up being my lack of the Datagrid.Columns tag. Thanks

Upvotes: 4

Phil
Phil

Reputation: 1973

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.

Upvotes: 2

Related Questions