Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if the UserControl is active other than using IsFocused

I am working on a WPF project, and I am trying to fire an event every time some userControls get active or inactive.

These userControls have many other controls inside of them.

I tried to achieve this using the userControl events GotFocus and LostFocus, but these events are not working in the way I need since the userControl loses the focus when I work with controls inside of it.

So, my question is: Is there a way to mantain a userControl as Active while the user works with controls inside of it, and, when the user goes to another userControl this first one gets Inactive???

Thank you in advance.

like image 885
Dante Avatar asked Nov 04 '22 21:11

Dante


1 Answers

I could solve my problem thank to the comments of @LPL and @Rachel.

I had to use the event UIElement.IsKeyboardFocusWithinChanged and it worked perfectly.

At first I had a problem which was that the callback method was being raised infinitely, but the actual problem was that I was showing a MessageBox every time the event IsKeyboardFocusWithinChanged raised, so, this caused that the IsKeyboardFocusWithin property changed and it created an infinite loop. But thanks to Rachel's advice I could figure out how to solve it.

like image 83
Dante Avatar answered Dec 29 '22 11:12

Dante