WPF复位聚焦按钮单击 [英] WPF Reset Focus on Button Click

查看:307
本文介绍了WPF复位聚焦按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框工具栏按钮。如果我打字的文本框和我点击按钮我要的文本框失去焦点这样的结合得到更新。我不想一个 UpdateSourceTrigger = PropertyChanged的添加到我的文本框。而是当我点击按钮重置焦点主窗口,我就这样失去什么都焦点并更新绑定。

I have a TextBox and a ToolBar with a Button. If I'm typing in the TextBox and I click the Button I want the TextBox to lose Focus so the binding gets updated. I don't want to add a UpdateSourceTrigger=PropertyChanged to my TextBox. But instead when I click on the Button I reset Focus to the main window so what ever I'm on loses Focus and updates the bindings.

我试着添加的OnClick 与下面的按钮,但它似乎并没有工作:

I've tried adding a OnClick to the button with the following, but it doesn't seem to work:

    private void Button_Click(object sender, RoutedEventArgs e) {
        FocusManager.SetFocusedElement(this, null);
    }

任何提示将AP preciated。

Any tips would be appreciated.

谢谢,
劳尔

推荐答案

的问题是,在工具栏放置在不同的 FocusManager.FocusScope 您的按钮。这意味着,无论是按钮文本框可在同一时间收到逻辑焦点,在各自的范围。这通常是一件好事,因为你通常不希望失去焦点在主窗口区当您选择菜单项和工具栏按钮,但在你的情况下,它是preventing您工作做什么。

The problem is that the toolbar places your button in a different FocusManager.FocusScope. That means that both the Button and the TextBox can receive logical focus at the same time, each in its own scope. This is normally a good thing, since you usually don't want to lose focus in your main window area when you select menu items and ToolBar buttons, but in your case it is preventing what you are doing from working.

虽然你可以覆盖工具栏上的 FocusManager.IsFocusScope 属性,并得到你想要的效果,这可能不是,因为它将使所有其他工具栏的最佳方案按键也从你的主窗口区域窃取焦点。

Although you could override the FocusManager.IsFocusScope property on the toolbar and get the effect you want, this is probably not the best plan since it would make all the other toolbar buttons also steal focus from your main window area.

相反,你可以用几个简单的解决方案之一:

Instead you could use one of several easy solutions:


  • 把你的按钮工具栏外

  • 一个调焦=真正的控件添加到您的主窗口区域和重点是单击按钮时

  • 通过调用手动强制更新 textBox.GetBindingEx pression(TextBox.TextProperty).UpdateSource()

  • 暂时在主窗口中的控制设置可调焦=真,将焦点设置到它,然后立即成立调焦=false的再次

这篇关于WPF复位聚焦按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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