简体   繁体   中英

WPF: bind SelectedItem of nested ListBox to property C#

i have main DataTemplate ListBox which has SelectedItem = SelectedSession and i have one more nested ListBox which has SelectedItem = AssignedExercises if i bind the property to both selected items, i can't get the nested ListBoxe's item AssignedExercises.

Do you have some idea how to reach the nested ListBoxes SelectedItem (AssignedExercises) and bind it to property?

XAML code:

<ListBox Grid.Row="1" ItemsSource="{Binding SessionList}" SelectedItem="{Binding SelectedSession}" Margin="0,0,0,20">
<ListBox.ItemTemplate>
    <DataTemplate>
        <Border CornerRadius="2" BorderBrush="Black" BorderThickness="2" Width="1000" Height="200" Margin="1">
            <Grid>

                <Label Content="List of exercises" RenderTransformOrigin="0.5,0.5" Height="30" VerticalAlignment="Top" FontWeight="Bold"></Label>

                <ListBox Height="150" Width="325" ItemsSource="{Binding AssignedExercises}" SelectedItem="{Binding AssignedExercises}">

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border Height="50" Width="300" BorderBrush="LightGray" BorderThickness="2" CornerRadius="2" Margin="0,1,0,1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40"/>
                                    </Grid.RowDefinitions>
                                    <StackPanel Grid.Row="0" Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                        <Label Content="{Binding Name}" FontSize="14" FontWeight="Bold"/>
                                    </StackPanel>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </Grid>
        </Border>
    </DataTemplate>
</ListBox.ItemTemplate>

The properties:

private TrainingSessionVM selectedSession;
private ExerciseVM selectedExercise;
public TrainingSessionVM SelectedSession
{
    get { return selectedSession; }
    set
    {
        selectedSession = value;
        RaisePropertyChanged();
    }
}
public ExerciseVM SelectedExercise
{
    get { return selectedExercise; }
    set
    {
        selectedExercise = value;
        RaisePropertyChanged();
    }
}

If I understand your question correctly, you have a list of Session , each of which contains a list of Exercise . Each of these could be considered a view model.

Your Main viewmodel has a SelectedSession to indicate which Session is selected. You just need to do the same thing within the Session (Not the main viewmodel as in your example). Add a SelectedExercise to Session , and bind SelectedItem of the inner ListBox to it.

You can then access the inner selected Exercise from the main viewmodel with SelectedSession.SelectedExercise .

Here i found the solution how to bind it directly through the DataContext binding:

SelectedItem="{Binding DataContext.SelectedExercise, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"

But in this case binding to the View Model should be done in this way:

<Grid.DataContext>
    <Binding Source="{StaticResource Locator}" Mode="OneWay" Path="TrainingSessionMain"/>
</Grid.DataContext>

Thx for participation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM