metal_man
metal_man

Reputation: 672

WPF Click on control inside ListBoxItem does not select ListBoxItem

Hi I could not find any similar problem so I posted new question. In code below I create ListBox control with ListBoxItems that each contains radio button inside. When I click on the radio button it gets selects but parent ListBoxItem does not (ListBoxItem is not highlighted). How can I solve this issue?

<ListBox Margin="0, 5, 0, 0" ItemsSource="{Binding mySource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Rabio template -->
            <RadioButton GroupName="radiosGroup"
                     Margin="10, 2, 5, 2"
                     Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedSetting}"
                     CommandParameter="{Binding SomeId, Mode=OneWay}"
                     Content="{Binding FileNameWithoutExtensions, Mode=OneWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 3

Views: 2724

Answers (2)

mjb
mjb

Reputation: 11

I have a listbox that displays ListBoxItems vertically, horizontally, and has all sorts of child buttons contained within each ListBoxItem.

The problem I ran into (like others) is when you click on a child button contained in the ListBoxItem, the ListBoxItem is not selected and you can not get the ListBoxItem.SelectedIndex value (because clicking on the button does not select the ListBoxItem).

I had some problems implementing the above xaml code because clicking on my GroupBox header would cause my selected ListBoxItem to lose focus.

The best solution I found on the web for this problem was to add a couple lines of code to the button's mouse click event to determine the parent control and then set the ListBoxItem.IsSelected = true.

After this is done, the ListBoxItem.SelectedIndex will contain the correct index value for the item selected. In my code, DataContext is set on the Listbox like this: DataContext="{StaticResource VM_Programs}"

Here's the VB code behind for the button event:

Private Sub YourButton_Click(sender As Object, e As RoutedEventArgs)
    Dim clicked As Object = (TryCast(e.OriginalSource, FrameworkElement)).DataContext
    Dim lbitem As ListBoxItem
    lbitem = YourListboxName.ItemContainerGenerator.ContainerFromItem(clicked)
    lbitem.IsSelected = True
    MsgBox("The listbox item (" + YourListboxName.SelectedIndex.ToString + ") is now selected")
End Sub

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18578

You can achieve this by applying the following ItemContainerStyle to your ListBox which uses Trigger on property IsKeyboardFocusWithin to select it.

<ListBox>
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
         <Style.Triggers> 
           <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
              <Setter Property="IsSelected" Value="True"/> 
           </Trigger> 
         </Style.Triggers>
       </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Upvotes: 7

Related Questions