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 TextBox has the name 'TextBox1'). How can I do this?
+1
A:
Hi FromTheMountain :).
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 :).
Anvaka
2009-10-23 10:59:42
Thanks a lot. I will try this out.
FromTheMountain
2009-10-23 11:29:11