I have very short contact with the C# and WPF, although most of the stuff I could find over the Internet. However, I cannot find anything (or don't know how to request google to find it) about blocking sending events to the parent.
I've got an Image inside ScrollViewer. My point is to create zooming option for the image by using Ctrl + mouse wheel, but obviously the scrollbars of the ScrollViewer are moving while I am moving mouse wheel (the mouse wheel method is defined within the Image). Is there any possibility to block event sent from child to parent when Ctrl is down?
In your handler for the Image, you should set the event's Handled property to true when Ctrl is pressed. This will prevent the ScrollViewer from handling the mouse wheel event.
See http://msdn.microsoft.com/en-us/library/ms742806.aspx for more information, especially the section entitled "The Concept of Handled."
Related
On System.Windows.UIElement there is a CaptureMouse() and a paired ReleaseMouseCapture() method. In this WPF DragDrop sample they call CaptureMouse on MouseDown and release it on MouseUp. The documentation in MSDN is about as useless as it comes - "CaptureMouse -> Captures the mouse."
In my head before trying it I assumed that it somehow locked the mouse inside the UIElement bounds, but that's clearly not the case when I try it. From experimenting, it seems to have something to do with responding to events when the mouse is outside of the UIElement, but not wanting to be a cargo cult programmer I don't want to just use it because the example does, I'd like an authoritative description of what it means.
From Capture and Uncapture the Mouse on MSDN:
When an object captures the mouse, all mouse related events are treated as if the object with mouse capture perform the event, even if the mouse pointer is over another object.
Only the capturing control receives the mouse events until released.
Capturing the mouse is useful for dragging because all the dragging code can exist in the one control, rather than being spread over multiple controls.
When it has captured the mouse, a control will receive mouse events even if the mouse pointer is no longer within its bounding area.
Typically, it's used for:
Drag and drop
Buttons (to handle Mouse Up when you put the mouse down on the button and move the mouse before you release the button)
The Silverlight 2 documentation for it has a more verbose description, I don't know why it isn't a part of the 3.5 documentation page too:
When an object has captured the mouse, that object receives mouse input whether or not the mouse pointer is within its bounding area. The mouse is typically only captured during simulated drag operations.
...
It works the same with WPF, and so the reason it is used with DragDrop, is that is how the
it knows to report back to the control being dragged from when the mouse may be outside of that control. If you comment out the MyCanvas.Capture() and the Capture(Null) (which clears it) then you can no longer drop.
Someone I've been banging my head around - is there a way to detect when intertial scrolling begins on a scrollviewer in WinRT's XAML model in a touch input scerario, no matter how round abouts it is?
I've tried listening to PointerUp & Down's & Released & Move events on UI elements and on the CoreWindow object itself and none of them fire after the scrollviewer starts moving (so it seems the scrollviewer is eating the pointer moved events before the application Window even recieves them? What fun DirectInput is...)? And of course overlaying something on top the of scrollviewer that catches events and then says it hasn't handled them simply doesn't work (I'm guessing a side effect of DirectInput again). So is there a way to do, without writing my own panel?
(I've been tasked to write a flipview like control where the items scale / zoom up as they become the active item - and it looks horrible to only fire it on the selection changed event).
I'm trying to write to a status bar the current mouse position and in order to that I took over OnMouseMove() event handler which is triggered when the mouse cursor enters my control. The problem is that in my control I have a WPF control which is has Dock.Fill Dockstyle, meaning, it fills the entire parent control.
When I run the applicaiton I see that nothing happens and the mouse position isn't updated, so I've noticed that the OnMouseMove() event of the WPF control is triggered and not the OnMouseMove() event of my control, which contains the relevant code for updating the mouse location coordinates.
I wanted to know if there's a way (other than implementing the code in the WPF OnMouseMove() event handler, of course) to bypass the WPF control event handler and use always my control's event handler.
I hope I was clear enough in my question, if not please let me know and I'll try to elaborate.
Thanks!
try the PreviewMouseMove-Version of OnMouseMove - this one should fire correct
and you need to have some kind of background (not null) or your controll won't see the mouse - so give it a transparent color or something)
I have a large user drawn control that fills most of the screen area in an application.
I would like to simulate some "onMouseHover" behaviour, I cant really use the userControl event as the mouse is almost always on that control so it fires all of the time.
How can I detect the mouse "hovering" over part of my user drawn control?
(If it helps an image of the app can be found at : http://www.benbun.co.uk/st3/ayv the control is the large "year calendar")
You could handle MouseMove events instead of MouseHover. Then you could calculate based on the X,Y location of the mouse whether or not the cursor is in the part of your control you are interested in creating "hover" behavior for.
I have a custom Canvas control (inherited from Canvas) overlaid over a large area of User Controls. The idea is to draw paths between user controls (i.e. connector lines).
To capture mouse movement, I call Mouse.Capture(theCanvas) on MouseDown. This works beautifully, but the user controls under the canvas obviously no longer receive mouse events. Mouse.DirectlyOver always shows the canvas, so I can't really fake it by peeking at the current position and seeing which user control it's over.
So, I still need the Canvas for drawing paths, but how can I solve this one of the following ways:
Peek under the Canvas and see what the topmost control is right under it?
Get this MouseDown -> Track MouseMoves -> MouseUp workflow to work on the canvas without mouse captures?
Any other ideas welcome...
I'd agree that those are your two options. If you want to only forward some clicks to your usercontrols, then go with option 1, and hit test the controls under the canvas.
If you need your usercontrols to behave as though there is nothing covering them (textboxes, buttons etc), then i'd recommend using the PreviewMouseMove event on the user control's parent, as this can pick up and optionally "handle" events before the controls get at the event, but it won't block the event if you don't set handled to true