How to adjust logical focus without focusing the keyboard?

How can I determine the logical focus code of a container or a focused child element WITHOUT giving it focus on the keyboard?

I just want to control which one will receive focus when the control receives focus via the Tab key or clicking on the part of the container that does not fall into the child element but does not give it (or steals) the actual focus, if I already have it.

And I also want that the choice of a specific child using keyboard gestures or clicking on it with the mouse is still possible.

I understand the difference in WPF between Focus Key and Logical Focus, as well as the fact that for an element that has Keyboard Focus, it also implies the presence of logical focus, but the presence of logical focus does not mean that the element has keyboard focus.

I also understand the attached property FocusManager.FocusedElement determines which element has logical focus in the visual tree, starting with the element defining this property.

Ive realized that this property is not used only when FocusManager.IsFocusScope is set to true, but also for containers such as GroupBox .

Ive made many attempts, tons of searches and reading in the WPF focus topic, but until I succeed, I don’t understand what I am missing:

Calling FocusManager.SetFocusedElement also gives keyboard focus, and I temporarily change the Focusable property of my child to false earlier, it only works the first time the child has not had focus before, but not after the child has received focus. Handling GotKeyboardFocus and PreviewGotKeyboardFocus in an element or container for overriding the initial focused element also does not work, because I can not determine whether the focus was obtained using the mouse or keyboard, and whether the focus was directly in the child element or indirectly through to Onteyner.

An example illustrating what I'm trying to achieve: Ive a simple RadioButtons group, and I want to dynamically control the code that will receive focus when the user β€œinserts” to move the focus to this GroupBox (usually this parameter that has isChecked=true )

 <GroupBox Header="Options" Name="myGroupBox" KeyboardNavigation.TabNavigation="Once" KeyboardNavigation. DirectionalNavigation="Cycle" > <StackPanel> <RadioButton GroupName="g1" Name="opt1" Content="Option _1"/> <RadioButton GroupName="g1" Name="opt2" Content="Option _2"/> <RadioButton GroupName="g1" Name="opt3" Content="Option _3"/> </StackPanel> </GroupBox> 

Last comment, I know how to implement a dynamic parameter list using a ListBox , bind the selectedItem of the list to the data context property, and then through the styles and templates in the ListBoxItem, associate the RadioButton's IsChecked property in the item template with the IsSelcted property of its parent ListBoxItem , and this works, but for my specific case, I need my RadioButtons to bind directly to the properties of my data context, I cannot bind them to the IsSelected property of the list at the same time.

+6
source share
1 answer

I know this is an old question, but hopefully this answer will help others with a similar problem.

If I understand the problem correctly, you can achieve the desired behavior by setting FocusManager.IsFocusScope = "True" in the GroupBox and connecting an event handler for the RadioButton.Checked event, which sets the logical focus to the event sender:

Xaml:

  <GroupBox Header="Options" Name="myGroupBox" FocusManager.IsFocusScope="True" RadioButton.Checked="MyGroupBox_OnChecked" KeyboardNavigation.TabNavigation="Once" KeyboardNavigation.DirectionalNavigation="Cycle"> <StackPanel> <RadioButton GroupName="g1" Name="opt1" Content="Option _1"/> <RadioButton GroupName="g1" Name="opt2" Content="Option _2"/> <RadioButton GroupName="g1" Name="opt3" Content="Option _3"/> </StackPanel> </GroupBox> 

Code behind:

  private void MyGroupBox_OnChecked(object sender, RoutedEventArgs e) { var radioB = sender as RadioButton; if (radioB != null) FocusManager.SetFocusedElement(myGroupBox, radioB); } 
+1
source

Source: https://habr.com/ru/post/914908/


All Articles