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);
        }
Avinash patil
  • 1,689
  • 4
  • 17
  • 39
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • What does your code behind look like? What are you trying to accomplish when the left mouse button is pressed? – Oren Hizkiya Apr 02 '14 at 13:52
  • can you please explain about what is the error? – Avinash patil Apr 02 '14 at 13:53
  • Why not use the click event? – Asik Apr 02 '14 at 13:59
  • 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. – Nico Jul 12 '21 at 13:34

3 Answers3

22

try simple PreviewMouseLeftButtonDown event

Avinash patil
  • 1,689
  • 4
  • 17
  • 39
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.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 10
    then WPF expect me to guess what event will work??!?! – Stav Alfi Apr 02 '14 at 14:01
  • 2
    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! – Dan Puzey Apr 02 '14 at 14:10
7

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

christiandersen
  • 193
  • 1
  • 5