¿Cómo navegar el cursor con TabIndex haciendo clic en el botón en wpf?

<Grid x:Name="LayoutRoot">
    <Button x:Name="BtnNavigateTabIndex" Content="NavigateTabIndex" Margin="152,200,0,190" HorizontalAlignment="Left" Width="120"/>
    <TextBox x:Name="TextBox_1" HorizontalAlignment="Left" Height="48" Margin="120,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
    <TextBox x:Name="TextBox_2" HorizontalAlignment="Left" Height="48" Margin="120,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
    <PasswordBox x:Name="PasswordBox_1" HorizontalAlignment="Left" Height="48" Margin="224,64,0,0" VerticalAlignment="Top" Width="72"/>
    <PasswordBox x:Name="PasswordBox_2" HorizontalAlignment="Left" Height="48" Margin="224,128,0,0" VerticalAlignment="Top" Width="72"/>
</Grid>

The design will will be like below.

enter image description here

If i click the button(NavigateTabIndex),The cursor should be navigate to TextBox or PasswordBox. For EX: If you click the Tab Key in you Keyboard,the cursor would navigate.This is the scenario what i need .

preguntado el 28 de agosto de 12 a las 12:08

2 Respuestas

In general there are a lot of gotchas with WPF focussing....some bugs even.

You need to understand the difference between Logical and Keyboard focus.

When using FocusScopes you can use:

MoveFocus con un TraversalRequest(FocusNavigationDirection.Next)

to change the logical focus to the next item in the scope.

An alternative to using FocusScopes is to track the GotFocus/LostFocus events and manage it yourself.

Algunos enlaces más:


Ok here's some example code for you. I took the liberty to redesign your use of the Grid a bit, to follow more conventional layout practice.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnNavigateTabIndex_Click(object sender, RoutedEventArgs e)
        {
            UIElement focussedelement = FocusManager.GetFocusedElement(grid1) as UIElement;

            bool bmovedfocus = focussedelement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

            if (bmovedfocus)
            {
                UIElement withfocusnow = FocusManager.GetFocusedElement(grid1) as UIElement;

                if (withfocusnow == focussedelement) // focus didn't change! because end of focus group..need to put it back to the start
                {
                    TextBox_1.Focus();
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TextBox_1.Focus();
        }
    }
}


    <Window x:Class="WpfApplication5.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication5"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <StackPanel>
            <Grid x:Name="grid1" FocusManager.IsFocusScope="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBox Margin="10" Grid.Row="0" Grid.Column="0" x:Name="TextBox_1" HorizontalAlignment="Left" Height="48" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
                <PasswordBox Margin="10" Grid.Row="0" Grid.Column="1" x:Name="PasswordBox_1" HorizontalAlignment="Left" Height="48" VerticalAlignment="Top" Width="72"/>
                <TextBox Margin="10" Grid.Row="1" Grid.Column="0" x:Name="TextBox_2" HorizontalAlignment="Left" Height="48" TextWrapping="Wrap" VerticalAlignment="Top" Width="80"/>
                <PasswordBox Margin="10" Grid.Row="1" Grid.Column="1" x:Name="PasswordBox_2" HorizontalAlignment="Left" Height="48" VerticalAlignment="Top" Width="72"/>
                <Button FocusManager.IsFocusScope="True" Padding="20" HorizontalAlignment="Center" Grid.Row="2" Grid.ColumnSpan="2" x:Name="BtnNavigateTabIndex" Content="NavigateTabIndex"  Width="120" Click="BtnNavigateTabIndex_Click" />
            </Grid>
        </StackPanel>
    </Window>

contestado el 23 de mayo de 17 a las 13:05

I tried this code this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); But it is not work.Please Post me the full program.It will much better. - ASHOK A

I am in basic in stage in wpf.I can't get your point. if You post the full code, it will help me. - ASHOK A

The above code is working well.Thanks for your valuable time. - ASHOK A

This line of code should set you in the right direction:

element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

where element is a UIElement (like one of your textboxes). This is an excerpt from the following post: Pasar al siguiente control al presionar la tecla Enter en WPF. And you put this code on the click event handler of the button...

By the way: discovering what the active UIElement / user control is can be done as described aquí.

contestado el 23 de mayo de 17 a las 12:05

i use this code element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));. But it is not work...Please post me the full program. - ASHOK A

Come'on ASHOKA a little more courage and effort to make this work. It's tooooo simple. I'm not gonna post the whole program :( - Bernoulli TI

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.