I have a TreeView
in which the items are defined by HierarchicalDataTemplate
s. Each TreeViewItem
that is created has some TextBox
es in it. When a TreeViewItem
is selected I want to set the Keyboard Focus
to a TextBox
of the TreeViewItem
(the TextBox
has the name TextBox1
). How can I do this?
Asked
Active
Viewed 4,289 times
2

Dave Clemmer
- 3,741
- 12
- 49
- 72

FromTheMountain
- 228
- 4
- 8
1 Answers
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 :).