Reputation: 5964
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
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.
Upvotes: 3
Views: 133
Reputation: 69985
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.
Upvotes: 1
Reputation: 1939
If you do not want to change anything in ViewModel, you can do this on XAML only:
<!-- Data1 -->
<ListBox x:Name="firstNamelbx"
Grid.Column="1"
DisplayMemberPath = "FirstName"
ItemsSource="{Binding ListData1 }" />
<!-- -->
<!-- Data2 -->
<ListBox x:Name="lbData2" Grid.Column="2"
DataContext="{Binding Path=SelectedItem, ElementName=firstNamelbx}"
DisplayMemberPath = "LastName"
ItemsSource="{Binding ListData2}" />
I think the better way is either you add a SelectedFirstName property in view model and bind it with first name list box's selected item, or use ICollectionView instead of List and sync its CurrentItem with first name list box.
Upvotes: 1