Use ListBox in Focus Area

Short story: The ListBox inside the focus area does not allow you to select an item.

Long story:

I am trying to create a context dependent widget for data entry. I have a main panel with several fields. Below, I have a panel with FocusManager.FocusScope="True" . This panel will be filled with the corresponding widgets for the field that is currently focused. For example, when you select a date field, a calendar will be displayed at the bottom of the screen.

I have several controls that require the user to select one of many values ​​from the list. I put the ListBox in the focus area, but I cannot select any items. When something is selected (programmatically) and you click on the ListBox, it selects everything.

I checked several events and did not collect MouseDown events, but it collects MouseMove events. It starts GotFocus whenever I click on an element, but it never starts LostFocus. I'm not sure what that means, but I hope this can help someone who reads it.

Here is the code I'm using to display a context sensitive widget. I have the following XAML in my window:

 <Grid x:Name="EntryWidget" FocusManager.IsFocusScope="True"> <Grid.Resources> <ListBox x:Key="List" ItemsSource="{Binding}" /> </Grid.Resources> </Grid> 

I use the Window.GotFocus routed event to update the widget to the corresponding control, for example:

 private void Window_GotFocus(object sender, RoutedEventArgs e) { FrameworkElement focus = (FrameworkElement)FocusManager.GetFocusedElement(this); EntryWidget.Children.Clear(); // Could this be the culprit? object tag = focus.Tag; if (tag != null) { if (EntryWidget.Resources.Contains(tag)) { EntryWidget.Children.Add(EntryWidget.Resources[tag] as UIElement); } } } 

So:

  • Is there a way to get ListBox to work in focus area?

  • Or is there another list control that works better inside the focus area?

  • Or am I using the wrong approach using focus areas? My requirements: The user should be able to select an item from the scroll list, which enters a value in the current field. The current field should not lose focus.

+7
source share
2 answers

My answer was not to use the focus area for this.

You achieve defeat the focus goal. What if someone cannot use the mouse and wants to use the keyboard instead?

Why not just select and then focus on the source control from the list, and not do all this extra work with magic tricks?

+2
source

When you click on a ListBox or the item that it contains, you set the keyboard focus of your application to the ListBox. This happens regardless of the focus areas that you have identified.

You can define the focus area for the top panel (the one that contains the fields), and so that the ListBox SelectionChanged sets focus to the top panel, which actually sets the focus on the item that was focused on it before clicking on the ListBox.

 <StackPanel> <StackPanel x:Name="upperPanel" FocusManager.IsFocusScope="True" GotFocus="upperPanel_GotFocus"> <TextBox x:Name="TextBox1"></TextBox> <TextBox x:Name="TextBox2"></TextBox> </StackPanel> <StackPanel> <ListBox x:Name="ListBox1" SelectionChanged="ListBox_SelectionChanged"> <ListBoxItem>First</ListBoxItem> <ListBoxItem>Second</ListBoxItem> <ListBoxItem>Third</ListBoxItem> </ListBox> <ListBox x:Name="ListBox2" SelectionChanged="ListBox_SelectionChanged"> <ListBoxItem>4</ListBoxItem> <ListBoxItem>5</ListBoxItem> <ListBoxItem>6</ListBoxItem> </ListBox> </StackPanel> </StackPanel> private IInputElement focusedElement = null; private void upperPanel_GotFocus(object sender, RoutedEventArgs e) { focusedElement = e.Source as IInputElement; } private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { UpdateTextBoxValue((e.AddedItems[0] as ListBoxItem).Content.ToString()); } private void UpdateTextBoxValue(string text) { TextBox focusedTextBox = focusedElement as TextBox; if (focusedTextBox != null) { focusedTextBox.Text = text; } upperPanel.Focus(); } 

The current active control in the upperPanel area upperPanel supported by the latest GotFocus event handler, and the UpdateTextBoxValue method UpdateTextBoxValue responsible for setting the text in the active TextBox control and setting focus on it.

I assume that you are familiar with the terms that I used (keyboard focus / logical focus); if not, you can look at the Focus Review or you can just ask.

+1
source

All Articles