Dante
Dante

Reputation: 3316

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.

Upvotes: 6

Views: 4231

Answers (2)

Dante
Dante

Reputation: 3316

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.

Upvotes: 2

yo chauhan
yo chauhan

Reputation: 12295

I am not sure but one workarounnd can be on lost focus of the control check if the control that got focus is child of your control if it is write just return if it is not then just write the logic what you want on lost focus of your control. I hope this will help

Upvotes: 0

Related Questions