I am working on a wpf application using PRISM. This is my first ever WPF application and I have found myself in a situation where I can’t go forward.
My scenario is this, I have a List of groups which I am binding with a ListBox, you can call it parent ListBox, each group object has a list of users associated to it and I am binding that list with another ListBox which is nested in the parent ListBox. Both bindings are working fine as you can see here:

I am facing two problems.
1. I can select both groups and individual users withing group separately but they are not synch, means If i select a user then the group which contains this user is not getting selected. I tried IsSynchronizedWithCurrentItem=”True” but that doesn’t seems to be working.
I would greatly appreciate if someone can point me out in a right direction how to achieve this or if there is any other way beside using ListBox within a ListBox.
2. I have a context menu associated with parent ListBox and I am able to successfully bind the commands with menus, but I am having troubles binding commands with nested ListBox Context Menu, here is my code
<ListBox x:Name="lstOfGroups"
ItemsSource="{Binding CurrentContest.Groups}"
SelectedItem="{Binding SelectedGroup}"
ItemTemplate="{StaticResource GroupTemplate}"
Style="{StaticResource ListBoxStyle1}"
ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
Background="Transparent" SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
Height="400">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" Width="1200"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Contestant" Command="{Binding AddGroupCommand}"/>
<MenuItem Header="Edit Contestant" Command="{Binding EditGroupCommand}"/>
<MenuItem Header="Delete Contestant" Command="{Binding DeleteGroupCommand}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
<DataTemplate x:Key="GroupTemplate" >
<Border x:Name="spPubItemBorder" Margin="3" BorderBrush="Black" BorderThickness="1" CornerRadius="10" Background="Honeydew">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tbGroupName" Grid.Column="0" Style="{StaticResource ItemTextBox}">
<TextBlock.Text>
<MultiBinding StringFormat="{}Group Name: {0}">
<Binding Path="Name" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Name="tbGroupAmount" Grid.Column="1" Style="{StaticResource ItemTextBox}">
<TextBlock.Text>
<MultiBinding StringFormat="{}Group Amount: {0}">
<Binding Path="Amount" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
<!--<ItemsControl ItemsSource="{Binding ContestantList}"
AlternationCount="2" ItemTemplate="{StaticResource ContestantTemplate}">
</ItemsControl>-->
<ListBox x:Name="lstOfContestant" Grid.Row="1"
ItemsSource="{Binding ContestantList}"
SelectedItem="{Binding SelectedContestant, ElementName=lstOfGroups}"
ItemTemplate="{StaticResource ContestantTemplate}"
Style="{StaticResource ListBoxStyleForContestant}"
ItemContainerStyle="{StaticResource ListBoxItemStyleForContestant}"
Background="Transparent" SelectionMode="Single"
Height="Auto">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" Margin="5" Width="375"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Add Contestant" Command="{Binding Path=DataContext.AddContestantCommand,ElementName=contestantManager}"/>
<MenuItem Header="Edit Contestant" Command="{Binding Path=DataContext.EditContestantCommand,ElementName=contestantManager}"/>
<MenuItem Header="Delete Contestant" Command="{Binding Path=DataContext.DeleteContestantCommand,ElementName=contestantManager}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</Grid>
</Border>
</DataTemplate>
I was wondering if someone can point me in a right direction here as well.
Thanks in advance.
For #1, set the
IsSelectedproperty of the group ListBox to true ifIsKeyboardFocusWithinThis will set the group’s
ListBoxItemas Selected when the keyboard focus is anywhere within theListBoxItemAs for #2, it sounds like you’re getting the wrong item, probably because you’re using
ElementNamein your binding, however the name is set on multiple items. Try using aRelativeSourcebinding to find theContextMenuitself, then bind to thePlacementTarget.DataContext