设置文本框焦点 [英] to set textbox Focus

查看:93
本文介绍了设置文本框焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户控件中,我具有控件(文本框)"textbox1"

In a usercontrol I have  a control (textbox) "textbox1"

然后我在窗口中调用了该用户控件

and I called this usercontrol in a window

我想通过窗口将焦点默认设置为 文本框(在xaml本身中,而不是后面的代码)

I want to set focus by default to  this  textbox (in xaml Itself ,not code behind) through the window (I have called the usercontrol)

我通过使用Focusmanager.FocusElement进行了尝试,但是我无法设置

I tried it by using Focusmanager.FocusElement ,but I can't able to set

请让我知道有关此问题的解决方案

Pls let me know the solution regarding this

预先感谢

努力可能会失败,但一定要努力-------------- sidd

Efforts may fail but don't fail to take effort -------------- sidd

推荐答案

您可以使用VisualTreeHelper查找文本框并设置焦点.

You can use VisualTreeHelper to find the Textbox and Set the Focus.

<UserControl x:Class="WpfApplication11.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>         
            <TextBox x:Name="Textbox1"  
                 Height="50"
                 Width="120"/>
    </Grid>
</UserControl>


MainWindow.xaml

MainWindow.xaml

<Grid>
        <local:MyUserControl x:Name="MyUserControl"/>
 </Grid>


设置焦点(MainWindow.xaml.cs )

Setting Focus (MainWindow.xaml.cs)

public partial class MainWindow : Window {

公共MainWindow() { InitializeComponent(); 已加载+ = MainWindow_Loaded; } void MainWindow_Loaded(对象发送者,RoutedEventArgs e) { var textBox = FindChild< TextBox>(this,"Textbox1"); textBox.Focus(); } 公共静态T FindChild< T>(DependencyObject parent,字符串childName)其中T:DependencyObject { 如果(父母==空) 返回null; T foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); 为(int i = 0; i< childrenCount; i ++) { var child = VisualTreeHelper.GetChild(parent,i); T childType =子项为T; 如果(childType == null) { foundChild = FindChild< T>(child,childName); 如果(foundChild!= null)中断; } 否则if(!string.IsNullOrEmpty(childName)) { var frameworkElement = child作为FrameworkElement; 如果(frameworkElement!= null&& frameworkElement.Name == childName) { foundChild =(T)child; 休息; } } 别的 { foundChild =(T)child; 休息; } } 返回foundChild; } }

public MainWindow() { InitializeComponent(); this.Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { var textBox = FindChild<TextBox>(this,"Textbox1"); textBox.Focus(); } public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject { if (parent == null) return null; T foundChild = null; int childrenCount = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = VisualTreeHelper.GetChild(parent, i); T childType = child as T; if (childType == null) { foundChild = FindChild<T>(child, childName); if (foundChild != null) break; } else if (!string.IsNullOrEmpty(childName)) { var frameworkElement = child as FrameworkElement; if (frameworkElement != null && frameworkElement.Name == childName) { foundChild = (T)child; break; } } else { foundChild = (T)child; break; } } return foundChild; } }

您可以使用它随时在代码中设置焦点.

You can use this to set the focus at any time in your code.

另一种方法很简单.如果只想在加载用户控件时第一次设置焦点,则只需在加载用户控件的事件中设置焦点即可.

An another way is pretty straight forward. If you just want to set the Focus for the first time when usercontrol is loaded you can simply set the focus in UserControl loaded event.

public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();
            this.Loaded += MyUserControl_Loaded;
        }

        void MyUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Textbox1.Focus();
        }
    }

祝你有美好的一天


这篇关于设置文本框焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆