0

I'm using WPF to design a borderless, movable application window.

In order to manually perform the ability to drag and drop the window, I've added an OnMouseDown event to the <Window> element, that executes a corresponding C# function (this.DragMove()).

Additionally, I need an <Image> button to allow some operation (with the OnMouseUp event this time). Note that it has to be an Image tag, and not a Button.

Unfortunately, the Image event fired only when the right mouse button is clicked, probably because the left button is held to the window event. Am I right?

When someone clicks the Image button, I want only the Image event to be triggered. How can I do it?

Reflection
  • 1,936
  • 3
  • 21
  • 39

2 Answers2

1

Its because of WPF bubbling and tunnelling events. so what u can do is whenever u handle event on button use bubbling for that means you can use previewevents for that for both button and window and whenever you just want to handle event for button then after last line of code in button click just write down like this.

e.handled=true;

// here e is the event argument which u will get in your preview event.so now window dragging event will not work.

i would just suggest first clear the idea of bubbling(preview mouse event) and tunneling in wpf.

Difference between Bubbling and Tunneling events

and go through some of the example of bubbling and tunnelling. you will get better idea.

http://www.codeproject.com/Articles/464926/To-bubble-or-tunnel-basic-WPF-events

Community
  • 1
  • 1
Ashok Rathod
  • 840
  • 9
  • 24
1

Problem you're facing is most probably related to event routing. The idea is that if your handler doesn't mark event as a Handled it will be routed to the next listener unless reach end of chain or one of listeners/handlers will set it as Hadnled.

So if you have MouseDown event handler for both Window and Image you should keep in mind that routing will stop at a point when you will set e.Handled = true;:

private void Window_OnMouseDown(object sender, KeyEventArgs e)
{
    e.Handled = false; // will NOT break event chain;
}

You can always check a type of sender so it will make possible for you to differ Image and Window events:

private void Image_OnMouseDown(object sender, KeyEventArgs e)
{
    if (sender is Image)
    {
         // Handle Image.MouseDown
         e.Handled = true; // we don't need to push event further;
    }
}
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
  • Your e.Handled solution really sounds like a working way, but unfortunately there is no change. Still, only when I omit the Window's event the Image's one is working. By the way, what I did is to assign True value to the Handled variable in the end of the Image's event function. It feels, probably just a wrong guess, like the event first to be fired is the Window's one. – Reflection Jul 07 '14 at 10:40
  • Now I found that replacing the Image's event to OnMouseDown like the Window's event (it was OnMouseUp as I wrote) it works. Then, why? And how to make it work as it is (I want the OnMouseUp event to simulate a Click event easily)? – Reflection Jul 07 '14 at 11:04
  • Well, I've solved it by binding an empty OnMouseDown event to the Image, that only make the Handled variable true, so it's not continuing to the Window's one. Is there a better way to do that? :) – Reflection Jul 07 '14 at 11:15
  • @Reflection Can you add a code sample? It will be easier to understand and help you to fix issue. – Anatolii Gabuza Jul 07 '14 at 11:25