-1

I have a TableData class:

public class TableData
{
    public string ID, WrestlerID;
    public string Name;
}

And some data that I then put on a list:

List<TableData> _tableData = new List<TableData>();
TableData tableData = new TableData
{
    ID = "0",
    WrestlerID = "000",
    Name = "test1"
};
_tableData.Add(tableData);
TableData tableData2 = new TableData
{
    ID = "1",
    WrestlerID = "111",
    Name = "test2"
};
_tableData.Add(tableData2);

I then iterate through my _tableData list and add each item on my DataGrid:

foreach (TableData data1 in _tableData)
{
    DGTable.Items.Add(data1);
}

BTW Here's my DataGrid:

<DataGrid x:Name="DGTable" Grid.Row="1">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="100"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="*"/>
        <DataGridTextColumn Header="Wrestler ID" Binding="{Binding WrestlerID}" Width="200"/>
    </DataGrid.Columns>
</DataGrid>

When I run the app, the DataGrid displays 2 rows but all fields are empty. Any thoughts? Thanks!

Julian
  • 5,290
  • 1
  • 17
  • 40
DC37
  • 71
  • 6
  • 1
    Your `TableData` class needs to implement the [INotifyPropertyChanged](https://learn.microsoft.com/dotnet/api/system.componentmodel.inotifypropertychanged) interface and your fields should be observable properties. – Julian Jun 01 '23 at 19:01
  • Thank you @ewerspej for that! Can you please show me how to do that through an answer? I'm pretty new on `INotifyPropertyChanged` and the `observable properties` part. – DC37 Jun 01 '23 at 19:03
  • Why are you populating the DataGrid in a loop? You could also bind to an [ObservableCollection](https://learn.microsoft.com/dotnet/api/system.collections.objectmodel.observablecollection-1) using the DataGrid's `ItemsSource` property instead of "manually" placing items from a List into the DataGrid: https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=windowsdesktop-7.0#binding-to-data – Julian Jun 01 '23 at 19:22

1 Answers1

0

Your TableData class needs to have properties instead of fields to be able use bindings.

It should also implement the INotifyPropertyChanged interface to use observable properties, so that changes to those properties get reflected in the UI.

Change your class as follows:

public class TableData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;  
  
    private void OnPropertyChanged([CallerMemberName] String propertyName = "")  
    {  
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }  

    private string id;
    public string ID
    {
        get => id;
        set
        {
            if(id == value) return;
            id = value;
            OnPropertyChanged();
        }
    }

    // repeat for WrestlerID and Name
    //...
}

Don't forget to add using System.ComponentModel; at the top.

Julian
  • 5,290
  • 1
  • 17
  • 40
  • This gave me an error `id was null` – DC37 Jun 01 '23 at 19:52
  • in the `if (id.Equals(value)) return;` part – DC37 Jun 01 '23 at 19:52
  • Right, because `id` isn't initialized, unassigned strings are `null`. For your purposes `id == value` should be sufficient, but you can also initialize it to `string.Empty`. I've updated the answer. – Julian Jun 01 '23 at 19:56
  • 1
    INotifyPropertyChanged is necessary only when *property changes* are expected (as the name suggests). Bindings will work with any public properties, observable or not. – ASh Jun 01 '23 at 20:52
  • Yes, but only once. Changes won't get reflected. I updated the answer for clarification. – Julian Jun 02 '23 at 06:06