开发者

WPF custom panel control doesn't respond to mouse events

I've created a custom panel control and would like to have it respond to a mouse move event, however, when I add an event handler like so:

Private Sub FloatingPanel_MouseMove(ByVal sender As Object, 
                                    ByVal e As MouseEventArgs) Handles Me.MouseMove

End Sub

It only responds when I move the mouse over one of the child controls within the panel. I need to have it respond whenever I move the mouse anywhere inside the custom panel.


Update: I found the following question which gave me a clue:

WPF - how to best implement a panel with draggable/zoomable children?

I can get mouse events on the GraphCanvas itself only if it has a background at the point

This led me to simply set the background which appears to have resolved the issue... My question now is, why? Why should I ha开发者_开发问答ve to set the background in order to receive a mousemove event?


Update 2: The following code is what ultimately solved the problem (See Kent's answer below).

Protected Overrides Function HitTestCore(ByVal hitTestParameters As System.Windows.Media.PointHitTestParameters) As System.Windows.Media.HitTestResult
    Return New PointHitTestResult(Me, hitTestParameters.HitPoint)
End Function

Thank you, Matt


For the purposes of hit testing, WPF's default hit testing logic has two modes of transparency. One is transparent both visually and to hit testing (#00000000 or by not setting a background at all), the other is transparent only visually and does not preclude hit testing (##00ffffff). You want the latter.

I believe you could also override UIElement.HitTestCore in your custom Panel such that there is no dependency on having the background set.


I actually suspected this might have been the issue here; If the background of a control is null and there is no other subcomponent there your mouse is not moving across the control but accross the control behind it, so it makes sense that you do not get a mouse event from that (it is not very expected though because the bounds of the control may envelope the area).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜