Search code examples
wpfmvvmdata-bindingdatagrid

WPF DataGrid data not showing unless binding mode changed


We have a DataGrid that is bound to an ObservableCollection of object 'Test', where Test inherits from INotifyPropertyChanged. However, despite the ObservableCollection being populated and OnPropertyChanged being run, the DataGrid is empty when run.

To make things more confusing, we have a very similar DataGrid, set up in the same way, but with slightly more complicated data and that works fine. Also, if we change the binding mode while debugging, the data appears in the table and can be updated as we would expect.

The Xaml for the DataGrid and the ObservableCollection property are copied below

<DataGrid Name="NonFunctioningTable" ItemsSource="{Binding PopulatedList, Mode=TwoWay}" Height="388" AutoGenerateColumns="False" IsReadOnly="True" CanUserAddRows="false" ColumnWidth="*" Margin="0,10,0,0">
<DataGrid.Columns>
    <DataGridTextColumn Header="Column1" Binding="{Binding ObjectProperty1}" />
    <DataGridTextColumn Header="Column2" Binding="{Binding ObjectProperty2}" />
</DataGrid.Columns>
</DataGrid>


private ObservableCollection<Test> populatedList;
public ObservableCollection<Test> PopulatedList
{
    get => populatedList;
    set
    {
        populatedList = value;
        OnPropertyChanged();
    }
}

Solution

  • We have solved this issue.

    A line in MainWindow.cs that had been added at the very start of development, before the addition of a ViewModel was setting the ItemSource of the table to null, removing the binding.