1

I have some user controls on a wpf form. I have set a trigger for them on IsMouseOverProperty so that every time the mouse is over any of them, the Background color get changed. I also have a trigger of IsPressedProperty so that every time that the user press the mouse button, the Background color get changed to another color.

Furthermore on the OnMouseLeftButtonDown event I set this. CaptureMouse() and in the OnPreviewMouseLeftButtonUp event I set this.ReleaseMouseCapture() so that when the user clicks anywhere else on the window, the the UserControl lost its mouse capture.

Every thing seems good so far unless when I press the mouse button on a user control and I move the mouse over the other user controls, their Background color never get changed because the mouse is captured by the control on which the mouse is captured.

Now I need to know is there any way to capture mouse on a user control and still be able to find if the mouse is over other controls?

Kalyan
  • 1,395
  • 2
  • 13
  • 26
user3530012
  • 720
  • 2
  • 20
  • 37
  • 1
    *Why* do you want to keep the background color of the control changed, even though you move the mouse away? It looks to me the solution to your problem is not really related to mouse events but to be found in the reason of *why* you want to do this (like some sort of drag'n'drop or drag'n'connect, or similar, perhaps?). –  May 13 '14 at 20:12
  • I think you misunderstood me. sorry for my bad grammar. I don't want to keep the background color change when the mouse is away. I want to keep the background color changed while the the mouse is over but the mouse is already captured by another control. – user3530012 May 13 '14 at 20:24
  • Can you please make your question very clear, because i am not sure whether i understand. Explain it step-by-step, like: 1. I click and hold down the mouse button on control A, and control A captures the mouse (and keeps it captured). 2. I move the mouse pointer (while mouse button is still pressed) over to control B (control A still has the mouse captured). 3. I want control B change its background color when the mouse moves over it. 4. Control B should revert back to original color when the mouse either leaves it or i depress the mouse button (in latter case control A also releases capture). –  May 13 '14 at 20:31
  • And also tell **WHY** you want to do that (there must be a reason, right?) –  May 13 '14 at 20:32
  • what you explained is exactly what I want unless in the step 4. in the step 4 Control B should revert back to original color only when the mouse either leaves it. when I depress the mouse button (control A releases catpture), control B still has the background color (I call it HoverBackgroundColor) if the mouse is over it. – user3530012 May 13 '14 at 20:37
  • I've just seen it in some windows applications. I'm trying to simulate that. that's all. I think it also worth learning new things. – user3530012 May 13 '14 at 20:38
  • Okay, than i would suggest to take a look at the approach given [here](http://stackoverflow.com/a/4861519/2819245). Although it demonstrates Mouse.MouseDownEvent, you can follow the same approach with the MouseMoveEvent or PreviewMouseMoveEvent. Whenever the mouse moves, you will then need to figure out the control underneath the mouse pointer, for which you should take a look [here](http://stackoverflow.com/questions/45813/wpf-get-elements-under-mouse). (For captured mouse, the sender of mouse events should always be the captor, hence this somewhat complicated way.) –  May 13 '14 at 20:48
  • ok. I'm goig to take look at them. thanks for your help. – user3530012 May 13 '14 at 20:53

0 Answers0