-1

enter image description here

i have two usercontrols which is bound in MainWindow. What i want when MainWindow is Loaded, textbox1 which is in usercontrol1 should be focused automatically, and when i hit down arrow from keyboard textbox2 which is in usercontrol2 should be focused. And again on up arrow focus textbox1.

Below is MainWindow's xaml code`

<Window x:Class="FocusTextboxesFromMain.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:FocusTextboxesFromMain" Loaded="Window_Loaded"
    mc:Ignorable="d"
    Title="Focus Textboxes Testing" Height="450" Width="800">
<Grid>
    <StackPanel>
        <local:UserControl1/>
        <local:UserControl2/>
    </StackPanel>
</Grid>

Below is Usercontrol1's xaml code`

<UserControl
x:Class="FocusTextboxesFromMain.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FocusTextboxesFromMain"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="800"
Height="200"
mc:Ignorable="d">
<Grid Background="Red">
    <TextBlock
        FontSize="50"
        Foreground="White"
        Text="UserControl1" />
    <TextBox
        x:Name="textbox1"
        Width="200"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        FontSize="25" />
</Grid>

and below is Usercontrol2's xaml code`

<UserControl
x:Class="FocusTextboxesFromMain.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FocusTextboxesFromMain"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="800"
Height="200"
mc:Ignorable="d">
<Grid Background="Green">
    <TextBlock
        FontSize="50"
        Foreground="White"
        Text="UserControl2" />
    <TextBox
        x:Name="textbox2"
        Width="200"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        FontSize="25" />
</Grid>

please note that i want all my logic in MainWindow Class, MvvM should not be involved

namespace FocusTextboxesFromMain
{
   using System.Windows;
   public partial class MainWindow : Window
   {
      public MainWindow()
       {
        InitializeComponent();
       }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           // Focus textbox1 of usercontrol1
        }

        // on down arrow then Focus textbox2 of usercontrol2
        // and again on up arrow then Focus textbox1 of usercontrol1
     }
}

sorry for bad explanation, thanks in advance

Usman Ali
  • 778
  • 8
  • 21

2 Answers2

0

You can find what you are looking for in the answer of this question:

Moving to next control on Enter keypress in WPF

Matteo
  • 2,029
  • 4
  • 22
  • 30
  • thanks, MvvM should not be involved, i have already read this post, it does not fulfill my requirements – Usman Ali Mar 22 '18 at 07:59
  • The answer did not require to involve MVVM. The example require only to edit the code behind and the App.xaml OnStartup method – Matteo Mar 22 '18 at 11:14
0

Just follow the code and create related event

public MainWindow()
    {
        InitializeComponent();
        this.PreviewKeyDown += new KeyEventHandler(txtFirst_KeyDown);
        this.PreviewKeyDown += new KeyEventHandler(txtSecond_KeyDown);

    }



    private void txtFirst_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Down)
        {               
            txtSecond.Focus();
        }

    }



    private void txtSecond_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Up)
        {
            txtFirst.Focus();
        }
    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        txtFirst.Focus();
    }
Raju Ahmed
  • 1,282
  • 5
  • 15
  • 24