Search code examples
c#wpfxamldatagridwpfdatagrid

Access specific list of lists in a DataGrid


I have a piece of data that is made up of List, which in turn has a List. How do I go about displaying a specific LeafSide?

ViewModel:

    private List<Leaf> _Leaves = new List<Leaf>();
    public List<Leaf> Leaves
    {
        get { return _Leaves; }
        set
        {
            _Leaves = value;
            NotifyPropertyChanged();
        }
    }

Data structure:

class Leaf : NotifyBase
{
    private string _LeafNo;
    public string LeafNo
    {
        get { return _LeafNo; }
        set
        {
            _LeafNo = value;
            NotifyPropertyChanged();
        }
    }
    private List<LeafSide> _Side = new List<LeafSide>();
    public List<LeafSide> Side
    {
        get { return _Side; }
        set
        {
            _Side = value;
            NotifyPropertyChanged();
        }
    }
    //More stuff
}
class LeafSide : NotifyBase
{
    private string _Notes;
    public string Notes
    {
        get { return _Notes; }
        set
        {
            _Notes = value;
            NotifyPropertyChanged();
        }
    }
    //More stuff
 }

View:

<DataGrid ItemsSource="{Binding Leaves}">
<DataGrid.Columns>
    <DataGridTextColumn Width="auto" MinWidth="50" Header="Notes" Binding="{Binding Side/Notes}"/>
    <DataGridTextColumn Width="50" Header="Punch" Binding="{Binding Side/Punch}"/>
    <!--More-->
</DataGrid.Columns>

The above DataGrid will display the first LeafSide perfectly fine, but I cannot for the life of me figure out how to get it to display the second side. All examples I can find of DataGrids and nested lists are to display specific values from a list. I expected something like

<DataGridTextColumn Width="auto" MinWidth="50" Header="Notes" Binding="{Binding Side[1]/Notes}"/>

to work, but it doesn't.

EDIT More info: I need the DataGrid to display: Seperate DataGrids for Leaf and LeafSide[0] displayed but also for LeafSide[1]


Solution

  • ASh provided the answer, just reposting so I can mark as Answered

    RIGHT {Binding Side[1].Notes}

    WRONG {Binding Side[1]/Notes}