使用 wpf 如何为放置在弹出窗口中的按钮启用“输入”键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14357495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:04:06  来源:igfitidea点击:

using wpf how to enable "enter" key press for a button placed inside a popup

wpfbuttonpopupwpf-controlsenter

提问by Sohail Faruqui

I am facing a problem though it might have a simple solution but was not able to find it out.

我面临一个问题,虽然它可能有一个简单的解决方案,但无法找到它。

I have a page (say mainPage) with several buttons inside that on which I enable "Enter" key-press for one button (say Submit) by setting the IsDefault="true" in xaml for that button and it worked fine.

我有一个页面(比如 mainPage),里面有几个按钮,我通过在 xaml 中为那个按钮设置 IsDefault="true" 来启用一个按钮的“Enter”按键(比如提交),它工作正常。

Clicking on the other buttons on my mainPage will open a popup which contain 2 buttons "Yes" and "No". setting IsDefault="true" property for "Yes" will not work when I press enter. (for information it will not work for the Submit button as well and that is also not a problem and I think I have a solutionfor that as well )

单击我的 mainPage 上的其他按钮将打开一个弹出窗口,其中包含 2 个按钮“是”和“否”。当我按 Enter 时,将 IsDefault="true" 属性设置为“是”将不起作用。(有关信息,它也不适用于“提交”按钮,这也不是问题,我想我也有解决方案

how should I enable this feature for the buttons in popup?

我应该如何为弹出窗口中的按钮启用此功能?

(for more info we have a button which create a popup that have button and textbox inside that.in that case setting IsEnable="true" for button inside the popup will do the job for the button but only if we click on the textbox. I tried setting focus on the textbox after observing this behavior so it might help but setting focus on the textbox does not work. use to set focus on textbox like this: Focusable="True" FocusManager.FocusedElement="{Binding ElementName=textboxName}" )

(有关更多信息,我们有一个按钮,它创建一个弹出窗口,其中包含按钮和文本框。在这种情况下,为弹出窗口内的按钮设置 IsEnable="true" 将为按钮执行此操作,但前提是我们单击文本框。观察此行为后,我尝试将焦点设置在文本框上,因此可能会有所帮助,但将焦点设置在文本框上不起作用。用于将焦点设置在文本框上,如下所示:Focusable="True" FocusManager.FocusedElement="{Binding ElementName=textboxName} " )

采纳答案by Rachel

You need to set focus on your Popupfirst so it can respond to the Enterkey

您需要将注意力集中在您的Popup第一个上,以便它可以响应Enter关键

There are two types of Focus in WPF: Keyboard and Logical. You can have multiple objects with Logical focus providing they're in different Focus Scopes (for example, you can have one element focused in your Window, and another in your Popup), but only one object can have Keyboard focus.

WPF 中有两种类型的焦点:键盘和逻辑。您可以拥有多个具有逻辑焦点的对象,前提是它们位于不同的焦点范围内(例如,您可以将一个元素聚焦在窗口中,另一个元素在弹出窗口中),但只有一个对象可以具有键盘焦点。

To set focus in a Popup, I usually just use the Loadedevent which executes anytime the Popup becomes visible and loads.

要在弹出窗口中设置焦点,我通常只使用Loaded在弹出窗口可见并加载时执行的事件。

MyTextBox.Focus();         // Set Logical Focus
Keyboard.Focus(MyTextBox); // Set Keyboard Focus

Edit

编辑

Sometimes if something else in your application is stealing focus right after the Loadedevent, you can use the Dispatcherto run your Focus code at a later DispatcherPriority, such as Input

有时,如果您的应用程序中的其他东西在Loaded事件发生后立即窃取焦点,您可以使用Dispatcher来在稍后的DispatcherPriority上运行您的焦点代码,例如Input

Dispatcher.BeginInvoke(DispatcherPriority.Input,
    new Action(delegate() { 
        MyTextBox.Focus();         // Set Logical Focus
        Keyboard.Focus(MyTextBox); // Set Keyboard Focus
     }));