3

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>

2 Answers 2

7

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>
Sign up to request clarification or add additional context in comments.

1 Comment

One problem with this is that it will unselect the item when it loses keyboard focus. This is not advisable if you have a Multi selectionMode.
0

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

1 Comment

I believe this question relates to C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.