繁体   English   中英

在Silverlight表单中将Enter键更改为Tab键

[英]Change Enter key to Tab key in silverlight forms

我如何将Silverlight表单中的Enter键更改为Tab键。我在Winforms中使用以下代码,但是我不知道如何在Silverlight中实现此功能!

/// <summary>
/// Change Enter key To Tab Key
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (msg.Msg == 256 && keyData == Keys.Enter)
    {
        // Execute an alternative action: here we tabulate in order to focus on the next control in the formular             
        if (ActiveControl.ToString().Contains("System.Windows.Forms.Button") ||
            ActiveControl.ToString().Contains("DevComponents.DotNetBar.ButtonX"))

            return base.ProcessCmdKey(ref msg, keyData);

        SendKeys.Send("{TAB}");

        // return true to stop any further interpretation of this key action
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

有点晚了,但是任何人都在查看此线程-通过在Silverlight 5中使用AutomationFactory按下ENTER键,可以使用TAB键。我通过编写一种行为来实现此目的,即监听KeyDown并传递TAB键。 该应用程序必须在OOB模式下运行。 您可以将此行为附加到Blend中的文本框。

public class EnterKeyPropertyChangeBehaviour : Behavior<DependencyObject>
{
    public EnterKeyPropertyChangeBehaviour()
    {

    }

    protected override void OnAttached()
    {
        base.OnAttached();

        // Insert code that you would want run when the Behavior is attached to an object.
        var fe = AssociatedObject as FrameworkElement;
        if (fe != null) fe.KeyDown += fe_KeyDown;

    }

    void fe_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
            {
                shell.SendKeys("{TAB}");
            }   

        }
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        // Insert code that you would want run when the Behavior is removed from an object.
        var fe = AssociatedObject as FrameworkElement;
        if (fe != null) fe.KeyDown -= fe_KeyDown;
    }

}

在Silverlight中,我们具有KeyDown事件,可以检查是否已按下哪个键。 您可以在KeyDown事件中编写一个函数,然后检查是否e.key == Enter,如果Enter键所按的焦点没有对准所需的所需文本框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM