How to detect mouse click position in Xamarin.Forms using WPF? - wpf

Using Xamarin.Forms with WPF, how do I capture a mouse click event and get the mouse position relative to the clicked element?
I've tried using ClickGestureRecognizer but can't get the event to fire.
<BoxView>
<BoxView.GestureRecognizers>
<ClickGestureRecognizer Clicked="ClickGestureRecognizer_OnClicked" />
</BoxView.GestureRecognizers>
</BoxView>
Perhaps this is not implemented for WPF at this time, or I am using it wrongly. TapGestureRecognizer does fire, but I can't see a way to get the mouse location when using that.

Related

WPF Popup not automatically closing when raised from a ListBoxItem

I have a User Control with an image in it and when the user clicks the image I use a popup to display a record card. I set StaysOpen="False" and all works perfectly through my application, except when I host it in a ListBox. In this case the Popup refuses to recognise a click away from it. Is there an issue with raising a popup from a ListBoxItem or are there other ways around this?
Maybe, opening Popup using Interactivity commands instead of using a Button could be the issue.
Interactivity is not doing the mouse capture and keyboard focus actions. So, Popup will not have a proper mouse capture reference by using Interactivity commands.
I did find the answer in the end and t was my mistake. The action to raise the popup was done on Mouse Down and the popup was opening and then closing on the mouse up cation. All I did was to change it to Mouse Up and it was fine. Thank you for your answer anyway.

previewmousewheel not getting raised when mouse is not on the control in wpf

I have a combobox control's previewmousewheel event been handled. When my mouse is on the control and i move the center wheel of my mouse then this event gets raised. But when my mouse is away from the control and i move the center wheel of my mouse then this event does not get raised.
Can anybody please explain me why is this happening?
If I want to raise an event when my mouse is away from the control then which event should I handle?
This is expected behaviour.
If the mouse cursor is not positioned over the control then no mouse events are routed through it. You wouldn't expect a mouse click event to be routed through it if the cursor was over a different control would you?
If you want this behaviour then I would suggest that you handle the mousewheel event in the page/view and route it from there, be cautious though as user expectation is for mouse and keyboard events to be handled by the in focus item.
In those cases that the user would expect the event to be forwarded to the control you could use Mouse.Capture(someControl) and Mouse.Capture(null) to stop the forwarding.
You should only use this when it makes sense. E.g. when dragging a scrollbar, when you started to drag on the thumb but are not required to stay on top of it as long as you keep the left mouse button down.
When using Mouse.Capture() make sure you always provide a way back from capturing.

Highlighting control when mouse is over child control

I have a silverlight templated control that changes opacity when you hover it . However when user points cursor to its child control the effect wores off. I want to have the control highlighted also when the user hovers any child control. I've did the same thing in WinForms by overriding the WndProc method. Is there something similar in silverlight ?
Thanks
Sounds to me like you have not used the correct events to detect the hover, I suspect you are using MouseMove. Instead Use MouseEnter and MouseLeave events. An MouseEnter event will occur when the mouse moves over the control. You move the mouse over child controls and you will get no further events. Then when the mouse moves completely out of your control you will get MouseLeave.

Set focus to another control after TreeView click

I have a TreeView control in a Windows application. I am opening another window from the TreeView click (Single Click) event (in tabbed environment, so all windows will appear as a tab in Visual Studio). I want to set focus to one control of the new window.
The problem is that, I am able to set the focus on the double click event of the TreeView. But same doesn't seem to be working with the TreeView single-click event.
Any workarounds?
Have you tried doing this in MouseUp instead? If that fails, there's always an easy (but disgusting) solution when it comes to UI issues like this: start a timer (with a running time of 10 ms or so) that will set the focus when it fires.

WPF MouseMove Event Polling

I've created a WPF application where the title bar and chrome are turned off. I have a border around the entire app, with the idea that it would act like the chrome in some regards. The first thing I'm trying to do is have the mousemove event capture the movement of the mouse when the mouse is clicked, so that the window moves with the mouse. The problem is that if the mouse moves too quickly, it manages to leave the window and therefore the mousemove no longer fires. I've been able to do this with a normal WinForm with no problems regardless of the speed of the mouse. Is there any way to do this more efficiently, or perhaps tune the polling of the mousemove? Perhaps a different container other than border that would perform better?
Try Me.DragMove in the window's left click event handler. It much better than most custom solutions.
When the user clicks you should capture the mouse (see Mouse.Capture). That way, you'll get the mouse events regardless of whether the mouse cursor is over your element or not.

Resources