简体   繁体   English

如何从另一个控件调用另一个控件的LostFocus事件

[英]How to call LostFocus event of another control from the other control

Calling the LostFocus event of control from other control 从其他控件调用控件的LostFocus事件

Example. 例。 : I want to call LostFocus event of Button1 button from the Gotfocus event of Button2 button. :我想从Button2按钮的Gotfocus事件中调用Button1按钮的LostFocus事件。

code is :- 代码是:-

private void button2_GotFocus(object sender, RoutedEventArgs e)
        {
          button1.gotFocus // this line giving error.
       }

Use following code: 使用以下代码:

private void button2_GotFocus(object sender, RoutedEventArgs e)
{
    button1.RaiseEvent(new RoutedEventArgs(LostFocusEvent, button1));
}


private void button1_LostFocus(object sender, RoutedEventArgs e)
{

}

If this doesn't solve your problem, post your code and state your problem and purpose clearly so that you can get better solution. 如果这样做不能解决您的问题,请发布代码并清楚说明问题和目的,以便获得更好的解决方案。

Try this 尝试这个

     private void button2_GotFocus(object sender, RoutedEventArgs e)
       { 
         button1_LostFocus(sender,e)

       }

you can just call event handler method of Button1 's LostFocus event in button2_GotFocus : 您可以只在button2_GotFocus调用Button1LostFocus事件的事件处理程序方法:

private void button2_GotFocus(object sender, RoutedEventArgs e) 
{ 
    button1_LostFocus(this.button1, null); 
}

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

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