mHelpMe
mHelpMe

Reputation: 6668

Binding list to datagrid

I have a WPF application with a datagrid. On load my ViewModel populates a list called HldChangeList. This list is bound to the data grid. The list contains approx. 200 items but at the moment the list shows 10 empty rows but no column headers. I've put a stop in my setter and can see the code is getting there. Not sure what else I am missing.

View Model

 private List<HoldingPrePost> _hldChangeList;

 public List<HoldingPrePost> HldChangeList
    {
        get
        {
            return _hldChangeList;
        }
        set
        {
            _hldChangeList = value;
            OnPropertyChanged("HldChangeList");
        }
    }

XAML

<DataGrid x:Name="dataGridHoldings"  
                  DataContext="{Binding HldChangeList}" 
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch"
                  Background="Silver"
                  RowHeaderWidth="30"  
                  ItemsSource="{Binding Path=HldChangeList, UpdateSourceTrigger=PropertyChanged}"
                  Style="{StaticResource DataGridTemplate}"  
                  ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"                                            
                  RowStyle="{StaticResource DG_Row}"
                  CellStyle="{StaticResource DG_Cell}"                                    
                  RowHeaderStyle="{StaticResource DG_RowHeader}"
                  Margin="15,5,20,15" >
            <DataGridTextColumn Header="ABC" Binding="{Binding ABC}"  IsReadOnly="True"/>
            <DataGridTextColumn Header="DEF" Binding="{Binding DEF}"  IsReadOnly="True"/>
            <DataGridTextColumn Header="GHI" Binding="{Binding GHI}"  IsReadOnly="True"/>
        </DataGrid>

Upvotes: 0

Views: 2064

Answers (2)

dkozl
dkozl

Reputation: 33394

You're setting both DataContext and ItemsSource to HldChangeList

<DataGrid 
    DataContext="{Binding HldChangeList}" 
    ItemsSource="{Binding Path=HldChangeList, UpdateSourceTrigger=PropertyChanged}"/>

WPF will search for HldChangeList items source property in current binding context which you also set to HldChangeList so in your case it will look for HldChangeList.HldChangeList property. If HldChangeList is already part of current binding context then you don't need to change DataContext otherwise you need to set it to something that contains HldChangeList property

EDIT

You forgot to enclose column definitions in DataGrid.Columns tag

<DataGrid x:Name="dataGridHoldings" ... ItemsSource="{Binding Path=HldChangeList}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ABC" Binding="{Binding ABC}"  IsReadOnly="True"/>
        <DataGridTextColumn Header="DEF" Binding="{Binding DEF}"  IsReadOnly="True"/>
        <DataGridTextColumn Header="GHI" Binding="{Binding GHI}"  IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

Upvotes: 3

Vinit Sankhe
Vinit Sankhe

Reputation: 19895

As dkozl said,

you need to set ItemsSource of your DataGrid explicitly without setting its DataContext or implicitly by setting the DataContext

Implicit

   <DataGrid ... 
          DataContext="{Binding HldChangeList}" ... 
          ItemsSource="{Binding}" ... />

Explicit

   <DataGrid ... 
          ItemsSource="{Binding HldChangeList}" ... />

Upvotes: 2

Related Questions