[英]How to keep text box's focus when a window pops up
i am building a virtual keyboard to suit the needs of the touch screen machine i'm going to be deploying on.我正在构建一个虚拟键盘,以满足我将要部署的触摸屏机器的需求。 i am using a popup window for the keyboard and have been able to wire all number buttons as follow, here's my virtual keyboard class
我正在为键盘使用弹出窗口,并且能够按如下方式连接所有数字按钮,这是我的虚拟键盘类
public partial class NumKeypad : Window
{
withoutB withoutbvn;
enterBvn ebvnn;
public NumKeypad()
{
InitializeComponent();
}
public NumKeypad(withoutB wobvn)
{
InitializeComponent();
withoutbvn = wobvn;
}
private void one_Click(object sender, RoutedEventArgs e)
{
var focusedElt = FocusManager.GetFocusedElement(withoutbvn);
var tbox = focusedElt as TextBox;
try
{
withoutbvn.ph.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;//this works, but this is assigning directly to only one control. i want to assign to whatever control that has focus
}
catch (Exception ex)
{
}
}
}
on the first line of the one_click function(which handles all input button click) i'm trying to get a reference to the element currently focused in the page whose instance is "withoutbvn".在 one_click 函数的第一行(处理所有输入按钮单击),我试图获取对当前在页面中聚焦的元素的引用,该元素的实例为“withoutbvn”。 on the second line, i am tryin to convert the element to a text box so i can write to its text property.
在第二行,我试图将元素转换为文本框,以便我可以写入其文本属性。 but that keeps returning null.
但这一直返回null。 meaning when this pop up windows come up(the keyboard pop up window comes up when a textbox or any other input element receives focus), i cannot get a reference to the focused textbox so i cannot write to it.
这意味着当这个弹出窗口出现时(当文本框或任何其他输入元素获得焦点时键盘弹出窗口出现),我无法获得对焦点文本框的引用,因此我无法写入。 Please how do i ensure a focused textbox remains focused so that i can assign its text property from a pop up window?
请问我如何确保聚焦的文本框保持聚焦,以便我可以从弹出窗口分配它的文本属性? Or if there's a better way to do this, pls point me in the right direction.
或者如果有更好的方法来做到这一点,请指出我正确的方向。 Thanks
谢谢
I've used this keyboard for WPF :我已将此键盘用于 WPF :
It is a popup control which can be customized as you wish.它是一个弹出控件,可以根据需要进行自定义。 You have the entire code and it's free.
你有完整的代码,而且是免费的。 In my case I had to adjust the popup (layout and to add the German letters) and was pretty straightforward.
就我而言,我不得不调整弹出窗口(布局并添加德语字母)并且非常简单。
I also had to show a numeric keyboard, and I've used the same keyboard but with a simpler layout.我还必须展示一个数字键盘,我使用了相同的键盘,但布局更简单。 Behind the scenes, all it is very simple: you have to define a key in a grid, place it where you want and make sure you generate on click the corresponding virtual key code.
在幕后,一切都非常简单:您必须在网格中定义一个键,将其放置在您想要的位置,并确保在单击时生成相应的虚拟键代码。
i used the popup control to create the keyboard, used buttons to create all keys and wired a single event handler to all input buttons, then different event handlers for the backspace and enter buttons.我使用弹出控件创建键盘,使用按钮创建所有键并将单个事件处理程序连接到所有输入按钮,然后为退格键和输入按钮使用不同的事件处理程序。 once any letter, number or symbol button is clicked, the following function gets called.
单击任何字母、数字或符号按钮后,将调用以下函数。
try
{
IInputElement focusedControl = Keyboard.FocusedElement;
var foc = focusedControl as TextBox;
foc.Text += (((sender as Button).Content as Border).Child as TextBlock).Text;
}
catch (Exception)
{
}
that inserts the button's text to the control in focus.将按钮的文本插入到焦点控件中。 This is pretty basic.
这是非常基本的。 i'll appreciate more suggestion on how i can expand on this.
我会很感激关于我如何扩展这一点的更多建议。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.