2

I have a TreeView in which the items are defined by HierarchicalDataTemplates. Each TreeViewItem that is created has some TextBoxes in it. When a TreeViewItem is selected I want to set the Keyboard Focus to a TextBox of the TreeViewItem (the TextBoxhas the name TextBox1). How can I do this?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
FromTheMountain
  • 228
  • 4
  • 8

1 Answers1

5

There are many ways you can do this. Here is just one of them. Use mine FocusExtension. IsFocused attached property. Bind it to TreeViewItem.IsSelectedProperty if you don't have a ViewModel underneath. Something like

<TextBox local:FocusExtension.IsFocused="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" />

should work. I typed that from head and didn't check the syntax. Be careful while copy-pasting :).

Community
  • 1
  • 1
Anvaka
  • 15,658
  • 2
  • 47
  • 56