[英]How to leave WPF UserControl somewhere inside UserControl?
我正在 WPF 中制作一個 UC (UserControl)。 我在 UC 中有一些文本框。 當用戶在其中之一上按下“返回鍵”時,我想離開 UC。 我希望返回鍵像主窗口中的 Tab 鍵一樣對待。 我該如何實施?
我在搜索中找到了這篇文章,它是我問題的答案。
這個想法是有一個名為 SendKeys* 的類,並在控件的 key down 事件中使用它的方法。
public static class SendKeys
{
/// <summary>
/// Sends the specified key.
/// </summary>
/// <param name="key">The key.</param>
public static void Send(Key key)
{
if (Keyboard.PrimaryDevice != null)
{
if (Keyboard.PrimaryDevice.ActiveSource != null)
{
var e1 = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, key) {RoutedEvent = Keyboard.KeyDownEvent};
InputManager.Current.ProcessInput(e1);
}
}
}
}
然后像這樣使用它:
private void textBoxesKeyDown(object sender, KeyEventArgs e) {
switch (e.Key)
{
case Key.Return:
SendKeys.Send(Key.Tab);
break;
}
}
*注意: SendKeys 是 WinForm 中的一個類,WPF 中不存在。 順便說一下,您可以使用這些代碼來實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.