LostFocus 处理问题

发布于 2024-11-25 08:38:01 字数 1868 浏览 1 评论 0原文

我想处理 TextBoxLostFocus 事件来执行一些操作。但如果 TextBox 失去焦点,我也不想执行该操作,因为单击了特殊的 Button (打开 OpenFileDialog)或Key.Enter 被捕获。当按下 Key.Enter 时,首先引发 KeyDown 事件。这是我的 KeyDown 事件处理程序:

public void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e != null && sender != null)
        if (e.Key == System.Windows.Input.Key.Enter && !String.IsNullOrWhiteSpace(((TextBox)sender).Text))
        {
            e.Handled = true;
            isEnterClicked = true;
            ((System.Windows.Controls.TextBox)sender).Visibility = System.Windows.Visibility.Collapsed;
        }
}

按下 Key.Enter 后,TextBox.Visibility 发生更改,并且此操作符将引发 LostFocus 事件。

public void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    try
    {
        if (!isEnterClicked)
        {
            DependencyObject dob = (DependencyObject)sender;
            while ( !(dob is ItemsControl))
            {
                dob = VisualTreeHelper.GetParent(dob);
            }
            dynamic myCmd = dob.GetValue(Control.DataContextProperty);
            myCmd.SomeCommand.Execute(((TextBox)sender).GetValue(Control.DataContextProperty));
        }
    }
    finally
    {
        isEnterClicked = false;
    }
}

LostFocus 处理程序首先观察 isEnterPressed 是否等于 false,这意味着 TextBox 失去焦点不是因为按下了 Enter。 SomeCommand 将删除绑定到 TextBox 的某些项目,并且它会消失。

问:那么,如何对 Button.Click 事件执行相同的操作?

首先,BEFORE Button 单击后,TextBox 失去了焦点。同样的方式也是不行的。 Button.Focusable="False"、创建新的 ControlTemplate 或处理 Timer.Elapsed 事件不满足我的要求。

I want to handle LostFocus event of TextBox to perform some actions. But I'm also wouldn't want to perform that actions if TextBox lost it focus because special Button (which opens OpenFileDialog) was clicked or Key.Enter was catched. When Key.Enter was pressed, first of all KeyDown event is raised. Here my event handler of KeyDown:

public void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e != null && sender != null)
        if (e.Key == System.Windows.Input.Key.Enter && !String.IsNullOrWhiteSpace(((TextBox)sender).Text))
        {
            e.Handled = true;
            isEnterClicked = true;
            ((System.Windows.Controls.TextBox)sender).Visibility = System.Windows.Visibility.Collapsed;
        }
}

After Key.Enter was pressed, TextBox.Visibility is changed, and this operator will raise LostFocus event.

public void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    try
    {
        if (!isEnterClicked)
        {
            DependencyObject dob = (DependencyObject)sender;
            while ( !(dob is ItemsControl))
            {
                dob = VisualTreeHelper.GetParent(dob);
            }
            dynamic myCmd = dob.GetValue(Control.DataContextProperty);
            myCmd.SomeCommand.Execute(((TextBox)sender).GetValue(Control.DataContextProperty));
        }
    }
    finally
    {
        isEnterClicked = false;
    }
}

LostFocus handler firstly watch whether isEnterPressed equals to false, its mean, TextBox lost it focus not because enter was pressed. SomeCommand will delete some item which was bind to TextBox, and it will disappear.

Q: So, how to do the same with Button.Click event?

First of all, BEFORE Button clicked, TextBox lost it focus. The same way is not acceptable. Button.Focusable="False", creating new ControlTemplate or handling Timer.Elapsed event do not satisfy my requirements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-12-02 08:38:01

如果我正确理解问题,请尝试检查按钮是否获得焦点,如果是,则不要在文本框丢失焦点事件中执行操作。如果我是正确的,按钮应该在文本框丢失焦点事件引发之前获得焦点。

if (!isEnterClicked && !button.Focused)
{
    //do stuff
}

If I understood problem correctly, try to check if button is focused, if so dont perform actions in textbox lostfocus event. If Iam correct button should be focused before textbox lostfocus event raised.

if (!isEnterClicked && !button.Focused)
{
    //do stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文