wpf: left button click is not recognized

It doesn't work, because the first fires is an event at the Button.Click, and when it works, it conflicts with the events like: MouseLeftButtonDown, MouseUp, MouseDown.

To make has worked this event, you need to define an PreviewMouseDown event, because it's a Tunnel event, this means that it will go down of the VisualTree hierarchy, therefore it is triggered before the Bubble events.

Also, as alternative, you can use the Button.Click event for Button.


MouseDown event is a bubbling event which bubbles from its originator to its root parent. But Click event eat up the MouseDown event and doesn't allow the event from bubbling upto Button.

You can use PreviewMouseDown event which is a tunnelling event which tunnels from root to its originator. So button will first get this event and then subsequent textBlock.

<Button PreviewMouseDown="Button_MouseDown">
   .......
</Button>

Refer to the snapshot below for the clear picture:

enter image description here


UPDATE

Hook only PreviewMouseDown event on button and remove handlers from individual textBlocks. Check for e.OrignialSource to see if TextBlock is actual original source or button.

private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    if (!(e.OriginalSource is TextBlock))
    {
        MessageBox.Show("You click on the button");
    }
    else
    {
        switch ((e.OriginalSource as TextBlock).Text)
        {
            case "First":
                MessageBox.Show("You click on first");
                break;
            case "Second":
                MessageBox.Show("You click on second");
                break;
            case "Third":
                MessageBox.Show("You click on third");
                break;
        }
    }
}

XAML

<Button PreviewMouseDown="Button_PreviewMouseDown" Height="57" Width="214">
    <WrapPanel>
        <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <TextBlock Foreground="Black" FontSize="24">First</TextBlock>
        <TextBlock Foreground="Red" FontSize="24">Second</TextBlock>
        <TextBlock Foreground="Blue" FontSize="24">Third</TextBlock>
    </WrapPanel>
</Button>