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

时间:2012-03-01 18:44:47

标签: c# .net silverlight

如何将Silverlight表单中的回车键更改为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);
}

2 个答案:

答案 0 :(得分:2)

有点迟了但有人查看了这个帖子 - 当你在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;
    }

}

答案 1 :(得分:1)

在Silverlight中我们有KeyDown事件,我们可以检查已按下哪个键。您可以在KeyDown事件中编写一个函数,并在该检查中是否e.key == Enter如果按Enter键而不是使焦点聚焦到您想要的Desired TextBox。