简体   繁体   English

TextBox LostFocus不会触发

[英]TextBox LostFocus does not fire

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 我有一个LostFocus事件的问题,当我单击背景时它不会触发。我读了一些有关焦点逻辑和键盘焦点的内容,但是当只有一个控件时,例如文本框,我找不到从控件中获取焦点的方法他们之中

XAML: XAML:

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

C#: C#:

    private void textBox1_LostFocus(object sender, RoutedEventArgs e)
    {

    }

You must use the following tunnelling event : PreviewLostKeyboardFocus on your textbox 您必须使用以下隧道事件:文本框上的PreviewLostKeyboardFocus

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. WPF中提供的输入事件通常实现为隧道/冒泡对。 Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs. 隧道事件有时也称为“预览”事件,因为该对使用了命名约定。

The following behavior will fix this: 以下行为将解决此问题:

public class TextBoxUpdateOnLostKeyboardFocusBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        if (AssociatedObject != null)
        {
            base.OnAttached();
            AssociatedObject.LostKeyboardFocus += OnKeyboardLostFocus;
        }
    }

    protected override void OnDetaching()
    {
        if (AssociatedObject != null)
        {
            AssociatedObject.LostKeyboardFocus -= OnKeyboardLostFocus;
            base.OnDetaching();
        }
    }

    private void OnKeyboardLostFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        var textBox = sender as TextBox;

        if (textBox != null && e.NewFocus == null)
        {
            // Focus on the closest focusable ancestor
            FrameworkElement parent = (FrameworkElement) textBox.Parent;
            while (parent is IInputElement && !((IInputElement) parent).Focusable)
            {
                parent = (FrameworkElement) parent.Parent;
            }

            DependencyObject scope = FocusManager.GetFocusScope(textBox);
            FocusManager.SetFocusedElement(scope, parent);
        }
    }
}

You can attach it to your TextBox as follows: 您可以将其附加到TextBox,如下所示:

 <TextBox>
    <i:Interaction.Behaviors>
        <behaviors1:TextBoxUpdateOnLostKeyboardFocusBehavior />
    </i:Interaction.Behaviors>              
</TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM