You can read about using '/' in bindings. See the "Current Position Indexes" section in this MSDN article.
Here is my solution:
Xaml
<Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Margin="5" Grid.Row="0" Grid.Column="0" Text="Mountains"/> <TextBlock Margin="5" Grid.Row="0" Grid.Column="1" Text="Lifts"/> <TextBlock Margin="5" Grid.Row="0" Grid.Column="2" Text="Runs"/> <ListBox Grid.Row="1" Grid.Column="0" Margin="5" ItemsSource="{Binding Mountains}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True" /> <ListBox Grid.Row="1" Grid.Column="1" Margin="5" ItemsSource="{Binding Mountains/Lifts}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True"/> <ListBox Grid.Row="1" Grid.Column="2" Margin="5" ItemsSource="{Binding Mountains/Lifts/Runs}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedRun}"/> </Grid>
C # (note that you do not need to implement INotifyPropertyChanged if the properties are not changed, not just selected)
public class MountainsViewModel { public MountainsViewModel() { Mountains = new ObservableCollection<Mountain> { new Mountain { Name = "Whistler", Lifts = new ObservableCollection<Lift> { new Lift { Name = "Big Red", Runs = new ObservableCollection<string> { "Headwall", "Fisheye", "Jimmy's" } }, new Lift { Name = "Garbanzo", Runs = new ObservableCollection<string> { "Headwall1", "Fisheye1", "Jimmy's1" } }, new Lift {Name = "Orange"}, } }, new Mountain { Name = "Stevens", Lifts = new ObservableCollection<Lift> { new Lift {Name = "One"}, new Lift {Name = "Two"}, new Lift {Name = "Three"}, } }, new Mountain {Name = "Crystal"}, }; } public string Name { get; set; } private string _selectedRun; public string SelectedRun { get { return _selectedRun; } set { Debug.WriteLine(value); _selectedRun = value; } } public ObservableCollection<Mountain> Mountains { get; set; } } public class Mountain { public string Name { get; set; } public ObservableCollection<Lift> Lifts { get; set; } } public class Lift { public string Name { get; set; } public ObservableCollection<string> Runs { get; set; } }
source share