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

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.

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.

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

Related

WPF Listview touch issue on window 10: "an event of another datatemplate is triggered while it may not happen"

When using a WPF application on windows 10 with a touchscreen we encounter an issue with the listview. When working with a mouse it works fine.
We have created a simple test-project, which is used to simulate the problem on windows 10 and can be found on GitHub. A ticket is also created on MSDN
In short below a summary of the technical setup:
We use a grouped listview, and for each group, a togglebutton and
another (inner) listview.
The inner listview uses an ItemTemplateSelector binded to an
Datatemplate selector, to choose an datatemplate.
There are 3 datatemplates (checkbox, numeric, text) that will be
choosen based upon the type property of the bounded model.
Each datatemplate has a stackpanel. The stackpanel in the text en
numeric datatemplate is wired to an PreviewMouseDown event.
Important, the stackpanel in the checkbox isn't wired to an event.
This works in general well, however sometimes, when touching the checkbox, the
PreviousMouseDown of another template is triggered.
I would expect that this behavior may not happen, is that correct?
We found a workaround (*) for this issue but we didn't find the root
cause.
Why is the event of another template triggered?
I'm starting to believe that this could be an issue with WPF Listview
and touch behavior?
(*) If we know that sometimes an event is triggered from a wrong template,
we verify every event whether that event is originated from the right template and if not we do nothing.
Below you can see when tapping quickly on the checkbox the clickevent get's triggered.
Below more details of the code:
The datatemplates and the selector
The grouped listview with the inner listview and the itemtemplate selector
Below the code behind and the handler for the PreviewMouseDown event
Below an overview of the steps we have been taken in order to resolve
it, but none lead into a solution.
Since WPF is supporting touch and a touch on a screen is also "translated" into a mousedown event, I don't see a problem why not using a previewMouseDown event on a touch screen. I also didn't find any offical documentation of Microsoft in not doing this.
Anyway, I could no longer reproduce the problem that a touch on listviewitem is invoking another previeuwMouseDown event of a template of another listviewitem in the list by ....
Changing the PrevieuwMouseDown event by a Touchdown event!
I am glad to find a solution, however based upon many online searches, I feel there are many issues with thouch on WPF and often it's is not clear what the rootcause is. Like in this case I found a solution by trial and error, but why the problem occurs when using previewMouseDown, is puzzling.

Stop event bubbling up using XAML

I have a Grid with a Button inside. The button has Flyout menu attached.
I implemented an action which opens the flyout menu when the button is tapped/clicked. This is the default behavior which does not require event writing. I also implemented an action when the grid is tapped/clicked.
The problem is that I do not want the grid to react when I tap/click the button. Based on this fine read, it all makes sense, but in my case, I do not have any code behind to add the e.Handled = true; line to.
Is there any way I could stop event bubbling up the tree using XAML only? Thanks!
While I hate to poach Gusdor's points. There is a built an enumeration property to deal with this types of situations called ClickMode which you can override the default mode for Button of Release and set it at the instance as ClickMode="Press" to get the desired effect and allow it to receive HitTestVisibility individually before any parent does.
Hope this helps, cheers.
I believe you will need to write some code but not the code behind you are trying to avoid.
Create an attached behavior that subscribes to, and handles the
bubbling event.
Attach the behaviour to the element where you want
the event bubbling to stop.
There is a Microsoft article about plugging Behaviours into UWP apps https://blogs.windows.com/buildingapps/2015/11/30/xaml-behaviors-open-source-and-on-uwp/

Deriving UIElement - how to receive focus

I created a class based on UIElement and my intention is to render it myself overriding OnRender. Rendering works fine. Next I want to implement focus management and continue with other aspects of LIFE, but overriding GotFocus and calling Me.Focus() in it don't do a single thing. I places my control on a Window with one another control - TextBox, and clicking on it doesn't do a single think. Tab doesn't set focus too, and TextBox is AcceptsTab negative. I know I will have to visualize focus somehow in OnRender to actually tell when the control is focused or not, but first I need to allow it to receive focus and that's where I struggle. Could you please help me out?
P.S. I tagged this with FrameworkElement because I don't have enough reputation to create a tag UIElement and leaving tags empty seemed like a silly thing to do.
Converting my comment into an answer:
I think you'd probably be better off deriving from FrameworkElement instead.

How can I determine when my StackPanel lost focus, from inside the UserControl that contains it?

I have a Silverlight UserControl, and it contains a StackPanel which, in turn, contains some UserControls in it. I would like to be able to determine when the user clicks/tabs outside of the outermost StackPanel (i.e. when the StackPanel loses focus), and I would like to be able to handle it from inside my UserControl. Does anyone have any suggestions on how I might accomplish that?
When one of the components lost focus, I tried dispatching a thread that would look to see if any controls within the main UserControl gained focus next, which would then tell me if I'm still inside my control. Trouble is, I was already on the UI thread, so the Dispatcher executed my action immediately, while I was still in the process of handling LostFocus. So, this didn't work.
Something else I tried involved using the FocusManager and determining if the control with focus was a child of my StackPanel. This didn't work because GetFocusedElement() returned the element whose focus I was in the process of losing.
Does anyone have any other ideas? Any help is appreciated.
This might not be the most elegant solution, but why don't you catch the OnLostFocus event on the StackPanel itself? In the handler, raise another event that your parent user control listens to. This way, you don't have to worry about dispatches or threads. Keeps it simple.

Hiding Popup control when other window's in focus

I have a custom UserControl which tries to recreate auto-complete for a textbox. When user types, the text is used to filter a provided collection of items and then a Popup displays a ListBox with items that match what user have typed.
Unfortunately, if user decides to switch away from the application to another window (browser, MSWord, anything!), the Popup remains on top of every other window!
Also, if I move my window (which hosts the custom control) with the popup open, the popup stays in place (and doesn't follow the window)! It's kinda funny but obviously not acceptable behaviour. I've looked around but only found one post about this that went unanswered for two years :(
Actually, I didn't realize that I had StaysOpen property of the Popup set to true.
<Popup StaysOpen="False" />
actually does the trick for me.
I had the same problem in a similar scenario. What I did was I subscribed to all posible "lost focus" events of the control and also got the window which hosts the control and subscribed to its GotMouseCapture and LocationChanged events. Event handlers of all those events are setting the popup's IsOpen property to false.
You can get the hosting window with this:
parentWindow = Window.GetWindow(this);
all other code is simply a lot of subscribing to events to do the same thing.
P.S. I'm not saying it's a pretty or optimal solution, but it works fine for me :)
According to the Popup documentation:
When Popup is displayed on the screen, it does not reposition itself if its parent is repositioned.
So it does not look like it would be a very good candidate for an autocomplete textbox. I think the class is meant more for showing information when you hover over an item.

Resources