弄清楚一个文本框正在失去焦点,其他控件将被集中

时间:2014-10-21 10:44:37

标签: c# windows-phone-8 textbox

所以基本上我有两个Textbox:LoginEmail和LoginPassword。我正在尝试为它们设置动画:

  • 用户输入LoginEmail#1动画#1启动,用户退出LoginEmail#1动画#2启动
  • 用户输入LoginEmail#1动画#1启动,用户退出LoginEmail#1并进入LoginPassword#2没有动画

代码

private void LoginEmail_GotFocus(object sender, RoutedEventArgs e)
{
    FocusAnimation.Begin();
}

private void LoginEmail_LostFocus(object sender, RoutedEventArgs e)
{
    UnfocusAnimation.Begin();
}

private void LoginPassword_GotFocus(object sender, RoutedEventArgs e)
{
    FocusAnimation.Begin();
}

private void LoginPassword_LostFocus(object sender, RoutedEventArgs e)
{
    UnfocusAnimation.Begin();
}

它现在无法正常工作,因为当用户输入LoginEmail#1然后转到LoginPassword#2时会有以下事件:

  • LoginEmail_GotFocus(=> FocusAnimation.Begin();)
  • LoginEmail_LostFocus(=> UnfocusAnimation.Begin();)
  • LoginPassword_GotFocus(=> FocusAnimation.Begin();)

因此有必要弄清楚用户是从LoginEmail转到LoginPassword并且没有显示UnfocusAnimation&第二焦点动画。不幸的是,我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:1)

您应该在LoginEmail TextBox之后检查谁获得焦点。 这样的事情应该有效:

private void LoginEmail_LostFocus(object sender, RoutedEventArgs e)
{
      var focusedControl = FocusManager.GetFocusedElement(this);
      if (focusedControl.GetType() != typeof(TextBox) || ((TextBox)focusedControl).Name != "LoginPassword")
      {
           UnfocusAnimation.Begin();            
      }
}

答案 1 :(得分:0)

只要在Textbox按下任何键,就可以获得keyboard 1的焦点:

yourtextbox.Focus();

然后失去focus你可以使用这样的东西:

this.Focus();

详细了解这些内容:

https://stackoverflow.com/questions/

How to remove the focus from a TextBox in WinForms?