WPF 在后台使TextBox失去焦点的方法

在软件设计开发的时候,偶尔会遇到在后台xaml.cs后台中,要将TextBox控件的焦点取消或者使TextBox控件获取焦点,下面介绍讲述一种简单的"只让特定的 TextBox 失去焦点"方法:

前端xaml代码示例:

XML 复制代码
<StackPanel Orientation="Horizontal">
	<TextBox Width="60" Height="30" x:Name="point12"/>
</StackPanel>

后端xaml.cs代码示例:在重载OnMouseMove事件中,使TextBox控件失去焦点。

cs 复制代码
protected override void OnMouseMove(MouseEventArgs e)
{
    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    {
	    // 找到你的TextBox
	    TextBox myTextBox = FindName("point12") as TextBox;
	    if (myTextBox != null)
	    {
		    myTextBox.Focusable = false;
		    myTextBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
		    myTextBox.Focusable = true;
	    }
    }));
}

软件运行后,鼠标左键点击,在TextBox输入框中,此时TextBox控件获取到焦点,鼠标移动后,焦点会自动消失。


相关推荐
bugcome_com10 小时前
WPF 命令 ICommand 从原理到实战
后端·wpf·icommand
武藤一雄1 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf
Venom841 天前
我的 WPF Powermill 工具
wpf
一念春风2 天前
证件照制作工具(WPF C#)
c#·wpf
He BianGu3 天前
【笔记】在WPF中GiveFeedbackEventHandler的功能和应用场景详细介绍
笔记·wpf
就是有点傻3 天前
WPF自定义控件-水晶球
wpf
He BianGu3 天前
【笔记】在WPF中QueryContinueDragEvent的详细介绍
笔记·wpf
He BianGu3 天前
【笔记】在WPF中QueryCursor事件的功能和应用场景详细介绍
笔记·wpf
He BianGu3 天前
【笔记】在WPF中CommandManager的功能和应用场景详细介绍
笔记·wpf
关关长语3 天前
HandyControl中Button图标展示多色路径
c#·.net·wpf·handycontrol