将文本框聚焦在从MainWindow控制的多个Usercontrol中

时间:2018-03-22 06:19:44

标签: c# wpf xaml

enter image description here

我有两个在MainWindow中绑定的用户控件。在MainWindow加载时我想要的是,usercontrol1中的textbox1应该自动聚焦,当我从键盘中按下箭头时,应该关注usercontrol2中的文本框2。再次在向上箭头焦点textbox1。

下面是MainWindow的xaml代码`

<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>

下面是Usercontrol1的xaml代码`

<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>

及以下是Usercontrol2的xaml代码`

<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>

请注意,我想要在MainWindow Class中使用我的所有逻辑,不应该涉及MvvM

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
     }
}

对不起解释,提前谢谢

2 个答案:

答案 0 :(得分:0)

您可以在这个问题的答案中找到您要找的内容:

Moving to next control on Enter keypress in WPF

答案 1 :(得分:0)

只需按照代码创建相关事件

即可
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();
    }