文本框引发LostFocus不火 [英] TextBox LostFocus does not fire

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

问题描述

我有LostFocus事件的一个问题它不火的时候,我点击background.I阅读焦点逻辑和键盘焦点一些东西,但我无法找到一种方法,当从控件获得焦点,一个像文本框有只是其中之一。



XAML:



<预类=郎咸平的XML prettyprint-覆盖> <电网HEIGHT =500WIDTH =500>
<文本框高度=23WIDTH =120保证金=12,12,0,0NAME =textBox1的引发LostFocus =textBox1_LostFocus/>
< /网格和GT;



C#:

 私人无效textBox1_LostFocus(对象发件人,RoutedEventArgs E)
{

}


解决方案

您必须使用以下隧道事件:PreviewLostKeyboardFocus你的文本框




隧道:最初,在元素树根事件处理程序
调用。路由事件再穿过沿线连续
子元素的路线,朝着那就是
路由事件源(引发该路由事件元素)的节点元素。
隧道路由事件经常被使用或处理作为
合成为一个控制的一部分,使得从复合部件事件可以
被故意地抑制或由特定于$ B $事件替换b中的完全控制。在WPF提供的输入事件往往会作为一个隧道/冒泡对实现
。隧事件也是
有时也被称为预览的事件,因为这是用于对一个命名
公约。



I have a problem with LostFocus event it does not fire when I click on the background.I read some stuff about focus logic and keyboard focus but I could not find a way to get the focus from a control a like textbox when there is only one of them

XAML:

<Grid Height="500" Width="500">
    <TextBox Height="23" Width="120" Margin="12,12,0,0" Name="textBox1" LostFocus="textBox1_LostFocus"  />
</Grid>

C#:

    private void textBox1_LostFocus(object sender, RoutedEventArgs e)
    {

    }

解决方案

You must use the following tunnelling event : PreviewLostKeyboardFocus on your textbox

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

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

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