This is a migrated thread and some comments may be shown as answers.

Tabbing between controls in a datatemplate

1 Answer 50 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 18 May 2012, 01:10 PM
Hi,

I have a treeview, with a hierarchical datatemplate, and then sub items are rendered with a data template which has a couple of text boxes for editing info on the treeview items. 

When I'm in the first textbox and press the tab key I want focus to move to the second textbox, but instead it jumps out of the treeview. How can I get this to work?

Here's what my datatemplate looks like:

<DataTemplate x:Key="GoverningBodyTemplate">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock VerticalAlignment="Center">Enrolment No:</TextBlock>
                    <TextBox VerticalAlignment="Center" Text="{Binding Path=EnrolmentNumber}" Width="100"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Visibility="{Binding Path=GoverningBody.RequiresBranch, Converter={StaticResource BoolToVisibilityConverter}}">
                    <TextBlock VerticalAlignment="Center">Branch No:</TextBlock>
                    <TextBox VerticalAlignment="Center" Text="{Binding Path=BranchNumber}" Width="100" TabIndex="1"/>
                </StackPanel>        
            </StackPanel>
        </DataTemplate>

Any help would be greatly appreciated!

John

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 23 May 2012, 07:09 AM
Hello John,

 You can use the PreviewKeyDown event of the TextBoxes and implement your own focus logic. For example like so:

<StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center">Enrolment No:</TextBlock>
                <TextBox VerticalAlignment="Center" Text="{Binding Path=ID}" Width="100" TabIndex="1" PreviewKeyDown="TextBox_PreviewKeyDown" />
            </StackPanel>
            <StackPanel Orientation="Horizontal"
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Tab)
    {
        (((((sender as TextBox).Parent as StackPanel).Parent as StackPanel).Children[1] as StackPanel).Children[1] as TextBox) .Focus();
        e.Handled = true;
    }
}
Similarly, you can use the event handler of the second TextBox and implement logic that moves the focus to the TextBox in the Template of the next RadTreeViewItem.
Hope this helps you. Kind regards,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
TreeView
Asked by
John
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or