How can I in code define the logical focus of a container, or focused child, WITHOUT giving it keyboard focus ?
I just want to control which child will get the focus when the control will get focus through Tab key or clicking on part of the container that does not hit a child, but not give it (or steal) actual focus if it doesn't already have it.
And I also want that selecting a specific child through a keyboard gesture or clicking on it with the mouse is still possible.
I understand the difference in WPF between Keyboard Focus and Logical Focus, and the fact that for an element, having Keyboard Focus implies also having logical focus, but having logical focus does not imply that the element have keyboard focus.
I also understand attached property FocusManager.FocusedElement defines which element has logical focus in the visual tree starting at the element defining this property.
I’ve realized that this property is not used only when FocusManager.IsFocusScope is set to true, but also for containers such as GroupBox.
I’ve made many attempts, tons of searches and reading on WPF focus topic, but with no success so far, I don't understand what I'm missing:
FocusManager.SetFocusedElement
also give keyboard focus, and it I temporarily change the property Focusable of my child element to false just before, it only works the very first time when no child had focus before, but not after a child got focus
An example to illustrate what I’m trying to achieve: I’ve a simple group of RadioButtons, and I want to control dynamically in code which option will get focus when user will “tab” to move focus to this GroupBox (typically the option 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>
A final comment, I know how to implement a dynamic list of options using a ListBox, binding selectedItem of the list to a property of the data context, and then through styles and templates on ListBoxItem bind IsChecked property of the RadioButton in item template to IsSelcted property of its parent ListBoxItem, and it works, but for my specific case, I need to keep my RadioButtons directly bound to properties of my data context, I can’t bind them to IsSelected property of the list at the same time.