Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding for nested data

Tags:

c#

binding

wpf

I have a class

public class MyData
{
    public string Caption { get; set; }
    public List<Data1> ListData1 {get; set;}
}

public class Data1
{
    public string FirstName { get; set; }
    public List<Data2> ListData2 { get; set; }
}

public class Data2
{
    public string LastName { get; set; }
}

data must be displayed in the following form

enter image description here

I do so - to link an MyData ListBox filled with data. For him, pointing DataTemplate, bring the code

            <!--  MyData  -->
            <Grid Grid.Column="0">

                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0" Text="{Binding Caption}" />

            </Grid>

            <!--  Data1  -->
            <ListBox 
                     Grid.Column="1"
                     DisplayMemberPath = "FirstName"
                     ItemsSource="{Binding ListData1 }" />
            <!--  -->        

            <!--  Data2  -->
            <ListBox x:Name="lbData2"
                     Grid.Column="2"
                     DisplayMemberPath = "LastName"
                     ItemsSource="{Binding ????}" />

        </Grid>
    </DataTemplate>

How to Make Binding for lbData2? ListData1.ListData2 option does not work.

like image 203
FetFrumos Avatar asked Dec 15 '25 16:12

FetFrumos


1 Answers

I believe that you can just use the following binding syntax:

ItemsSource="{Binding ListData1/ListData2}" 

This means bind to the ListData2 property of current or selected instance of ListData1.

You can find out more from the Binding.Path Property page at MSDN.

like image 136
Sheridan Avatar answered Dec 17 '25 05:12

Sheridan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!