开发者

Writing <period> in my TextBox should perform tab

When I write a <period> into myTextBox I want a myOtherText.Focus to be performed.

Anyone know a way of doing this开发者_StackOverflow in xaml code?

Edit:Agree that the orginale solution I asked for was a little hacky. So I changed the question. :)


I don't think you could pull this off with XAML. Not in any sane way at least.

What you can do is to listen to KeyDown or KeyUp events on your textbox control and move the focus accordingly. Depending on whether or not you want the dot to appear or not, you'd choose which of these events to listen to:

  • To have the dot appear in the first (original) textbox, listen to KeyUp and move focus when e.Key == Key.0emPeriod
  • To have the dot appear in the second textbox, listen to KeyDown and move focus when e.Key == Key.0emPeriod
  • To not have the dot appear at all, listen to KeyDown and move focus when e.Key == Key.0emPeriod and also set e.Handled to true after moving focus (in the KeyDown event handler)

This code can move focus to whatever is next in the tab order:

private void MoveFocus(TextBox from)
{
    // Assuming the logical parent is a panel (grid, stackpanel etc.) and 
    // that the element to move focus to is also in the same panel after 
    // the current one
    // This will break if you have a more complex layout
    var panel = from.Parent as Panel;
    if (panel == null)
        return;
    var pos = panel.Children.IndexOf(from);
    for (int i = pos + 1; i < panel.Children.Count; i++)
    {
        UIElement child = panel.Children[i];
        if (KeyboardNavigation.GetIsTabStop(child))
        {
            child.Focus();
            break;
        }
    }
}

of course, if you always know the name of the control to move focus to, you can just say:

myOtherTextBox.Focus();

instead of navigating through the panel's children collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜