Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpf:无法识别左键单击_C#_Wpf_Events_Mouseevent - Fatal编程技术网

C# wpf:无法识别左键单击

C# wpf:无法识别左键单击,c#,wpf,events,mouseevent,C#,Wpf,Events,Mouseevent,我刚进入WPF,当我学习到这些材料时,我面临着一个奇怪的问题 我构建了一个按钮,包含带有文本块的层,我想识别用户在“第一”、“第二”或“第三”(我输出一条消息)上单击按钮本身的位置 除了当用户单击左键(仅使用中键或右键)时,按钮不会引发事件外,所有操作都正常 所以我的问题是:当我用鼠标左键按下按钮时,为什么没有收到消息框(而我用其他鼠标按钮收到消息框) XAML: <Button Margin="145,152,144,102" Padding="5,5,5,5" HorizontalA

我刚进入WPF,当我学习到这些材料时,我面临着一个奇怪的问题

我构建了一个按钮,包含带有文本块的层,我想识别用户在“第一”、“第二”或“第三”(我输出一条消息)上单击按钮本身的位置

除了当用户单击左键(仅使用中键或右键)时,按钮不会引发事件外,所有操作都正常

所以我的问题是:当我用鼠标左键按下按钮时,为什么没有收到消息框(而我用其他鼠标按钮收到消息框)

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>
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)
{
    // This event not working good
    // only middle & right mouse buttons are recognized
    MessageBox.Show("You click on the button");
}

谢谢大家!

事件是一个
冒泡事件
,它从其发起者冒泡到其根父级。但是,
单击
事件会吃掉
鼠标向下
事件,并且不允许事件冒泡到按钮

您可以使用
PreviewMouseDown
事件,这是一个
隧道事件
,它从根目录隧道到其发起者。所以按钮将首先获取此事件,然后获取后续的文本块

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


弗斯特
第二
第三

它不起作用,因为第一次触发的是
按钮上的事件。单击
,当它起作用时,它与以下事件冲突:
MouseLeftButtonDown
MouseUp
MouseDown

要使此事件生效,您需要定义一个
PreviewMouseDown
事件,因为它是一个
Tunnel
事件,这意味着它将进入VisualTree层次结构,因此在气泡事件之前触发


另外,您也可以使用
按钮。单击按钮的事件。

非常感谢,但我尝试了一下,只得到了按钮事件(而不是文本块事件)。如果我在文本块中将触发器更改为
PreviewMouseDown
,每次单击我都会收到两个消息框(我只需要一个消息和一个真实消息)@AsfK:Hook only
PreviewMouseDown
事件打开按钮,而不是文本块,并在处理程序中检查
e.OriginalSource
,以查看事件的发起人。
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;
        }
    }
}
<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>