I am recently in WPF and when I study the material, I came across a strange problem.
I create a button that contains layers with a text block, and I want to find out where the user clicks on the button itself, on the "first", "second" or "third" (I display a message).

Everything works fine, except that the button does not trigger an event when the user clicks the left button (just the middle or right button).
so my question is: why didn’t I get a message box when I click on the button itself with the left mouse button (and I get a message box with other mouse buttons)?
XAML:
<Button Margin="145,152,144,102" Padding="5,5,5,5" HorizontalAlignment="Center" VerticalAlignment="Center" MouseDown="Button_MouseDown" Height="57" Width="214"> <WrapPanel> <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center"></WrapPanel> <TextBlock Foreground="Black" FontSize="24" MouseDown="TextBlockFirst_MouseDown" >First </TextBlock> <TextBlock Foreground="Red" FontSize="24" MouseDown="TextBlockSecond_MouseDown">Second </TextBlock> <TextBlock Foreground="Blue" FontSize="24" MouseDown="TextBlockThird_MouseDown" >Third </TextBlock> </WrapPanel> </Button>
The code:
private void TextBlockFirst_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on first"); } private void TextBlockSecond_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on second"); } private void TextBlockThird_MouseDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("You click on third"); } private void Button_MouseDown(object sender, MouseButtonEventArgs e) {
Thanks!
c # events wpf mouseevent
Asfk
source share