wpf Button.MouseLeftButtonDown doesnt work at all
Asked Answered
I

3

11

Im trying to learn how MouseLeftButtonDown works but no seccuss until now.

When i click on the button, nothing heppends.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Name="sss">
            <Button x:Name="b1" Height="213" MouseLeftButtonDown="sss_MouseDown"/>
        </StackPanel>
    </Grid>
</Window>

Code behind is :

private void sss_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("3   ->>>>>" + ((Control)sender).Name);
        }
Instantaneous answered 2/4, 2014 at 13:50 Comment(4)
What does your code behind look like? What are you trying to accomplish when the left mouse button is pressed?Loewi
can you please explain about what is the error?Aspectual
Why not use the click event?Quibbling
I have the same issue and I wonder why this event even exists if you can't use it at all. It is even worse: if you try to use the MouseButtonDown event, this will work for all mouse buttons except the left mouse button. Thanks, Microsoft, great design choice.Gilmer
R
9

From the documentation on this event:

Some control classes might have inherent class handling for mouse button events. The left mouse button down event is the most likely event to have class handling in a control. The class handling often marks the underlying Mouse class event as handled. Once the event is marked handled, other instance handlers that are attached to that element are not ordinarily raised. Any other class or instance handlers that are attached to elements in the bubbling direction towards the root in the UI tree are also not ordinarily raised.

So in short: the button is likely handling this event in order to generate its own MouseDown and MouseClick events. Because the button is marking the event as handled, your own handler isn't being called. Try using one of the more standard events instead.

The page also lists a couple of workarounds, but typically I'd steer clear of these and use the more standard solutions.

Richardson answered 2/4, 2014 at 13:59 Comment(2)
then WPF expect me to guess what event will work??!?!Instantaneous
No, it expects you to read its documentation. Typically, you would use the Click event for interactions with a button - using MouseLeftButtonDown is a pretty rare thing to come across!Richardson
A
22

try simple PreviewMouseLeftButtonDown event

Aspectual answered 2/4, 2014 at 13:59 Comment(1)
yes your right but what is there no way I can use MouseLeftButtonDown?Instantaneous
R
9

From the documentation on this event:

Some control classes might have inherent class handling for mouse button events. The left mouse button down event is the most likely event to have class handling in a control. The class handling often marks the underlying Mouse class event as handled. Once the event is marked handled, other instance handlers that are attached to that element are not ordinarily raised. Any other class or instance handlers that are attached to elements in the bubbling direction towards the root in the UI tree are also not ordinarily raised.

So in short: the button is likely handling this event in order to generate its own MouseDown and MouseClick events. Because the button is marking the event as handled, your own handler isn't being called. Try using one of the more standard events instead.

The page also lists a couple of workarounds, but typically I'd steer clear of these and use the more standard solutions.

Richardson answered 2/4, 2014 at 13:59 Comment(2)
then WPF expect me to guess what event will work??!?!Instantaneous
No, it expects you to read its documentation. Typically, you would use the Click event for interactions with a button - using MouseLeftButtonDown is a pretty rare thing to come across!Richardson
A
7

I had the same problem but used the PreviewMouseLeftButtonDown event instead. That worked.

Albinus answered 22/6, 2016 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.