Reflection
Reflection

Reputation: 2106

XAML Window MouseDown event beats Image event

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?

Upvotes: 0

Views: 1327

Answers (2)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

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;
    }
}

Upvotes: 1

Ashok Rathod
Ashok Rathod

Reputation: 840

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

Upvotes: 1

Related Questions