Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody

Is it possible to realise a ListBox which allows selecting items by the mouse but does not take the keyboard focus? When I select a filter item, the ListBox (or its ScrollViewer?) take the keyboard focus.

Aim
I have lists with filters and a "main" list which shows the data based on the selected criteria. The user should be able to select those filters without loosing the keyboard focus of the main list, as this would force him, to click again on the main list after every change of the filters. However, mouse scrolling on the filter lists should still be possible.

This is my listbox:
XML
<ListBox ItemsSource="{Binding FiltersWebsite}">
                               <ListBox.Style>
                                   <Style TargetType="ListBox">
                                       <Setter Property="ItemContainerStyle">
                                           <Setter.Value>
                                               <Style TargetType="{x:Type ListBoxItem}">
                                                   <Setter Property="IsSelected"
                                                           Value="{Binding IsSelected}" />
                                                   <Setter Property="KeyboardNavigation.TabNavigation"
                                                           Value="None" />
                                                   <Setter Property="KeyboardNavigation.IsTabStop"
                                                           Value="False" />
                                                   <Setter Property="Template">
                                                       <Setter.Value>
                                                           <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                                               <Border Focusable="False">
                                                                   <ContentPresenter />
                                                               </Border>
                                                           </ControlTemplate>
                                                       </Setter.Value>
                                                   </Setter>
                                               </Style>
                                           </Setter.Value>
                                       </Setter>
                                       <Setter Property="ItemTemplate">
                                           <Setter.Value>
                                               <DataTemplate>
                                                   <TextBlock Text="{Binding Description}" />
                                               </DataTemplate>
                                           </Setter.Value>
                                       </Setter>
                                   </Style>
                               </ListBox.Style>
                               <i:Interaction.Triggers>
                                   <i:EventTrigger EventName="SelectionChanged">
                                       <i:InvokeCommandAction Command="{Binding RefreshViewCommand}" />
                                   </i:EventTrigger>
                               </i:Interaction.Triggers>
                           </ListBox>


I know, I could simply set the focus on the main list with [NAMEOFLISTBOX].Focus, but this is not so elegant and would be hard to maintain, as I use the same filter list in several windows.

I have tried for several weeks to get this to work but did not succeed. Has anyone suggestions?

Thank you in advance.


Regards
seb
Posted
Comments
ashok rathod 6-Oct-14 23:00pm    
May be this will help http://stackoverflow.com/questions/5377474/wpf-how-to-make-a-listbox-listview-unfocusable
Member 9381771 9-Oct-14 13:02pm    
Dear ashok

Thank you for your reply. Unfortunately none of the solutions shown in this thread work for my case, as my listitems should still be selectable with the mouse. Furthermore setting "IsSelected" by code, is not really an option, as I use a binding for the items (and the selected items) of the list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900