Home > Net >  How to correctly handle keyboard in TreeView?
How to correctly handle keyboard in TreeView?

Time:09-10

I have a visual tree similar to the following one:

TreeView
    TreeViewItem
        ComboBox
    TreeViewItem
        TextBox

The visual tree is made from a few data templates, and a single DataTemplateSelector.

The problem, when I press or on the keyboard when the element with the ComboBox has focus, ComboBox handles that key by switching the selected element.

How to prevent ComboBox and TextBox controls inside the tree nodes from handling the keys which are used by TreeView keyboard navigation?

CodePudding user response:

You can disable KeyboardNavigation.DirectionalNavigation within TreeViewItem containers.

Gets or sets the directional navigation behavior for the children of the element that this property is set on. [...] Directional navigation is invoked by using the arrow keys.

<TreeView.ItemContainerStyle>
   <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
      <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="None"/>
   </Style>
</TreeView.ItemContainerStyle>
  • Related