wpf:无法识别左键单击 [英] wpf: left button click is not recognized
问题描述
我刚接触 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 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" >第三 </TextBlock></WrapPanel></按钮>
代码:
private void TextBlockFirst_MouseDown(object sender, MouseButtonEventArgs e){MessageBox.Show("你先点击");}私有无效 TextBlockSecond_MouseDown(对象发送者,MouseButtonEventArgs e){MessageBox.Show("你点击第二个");}私有无效 TextBlockThird_MouseDown(对象发送者,MouseButtonEventArgs e){MessageBox.Show("你点击第三个");}私有无效Button_MouseDown(对象发送者,MouseButtonEventArgs e){//这个事件效果不好//只有中间 &鼠标右键被识别MessageBox.Show("你点击了按钮");}
谢谢!
MouseDown
事件是一个冒泡事件
> 从它的发起者冒泡到它的根父级.但是 Click
事件会占用 MouseDown
事件,并且不允许该事件冒泡到 Button.>
您可以使用 PreviewMouseDown
事件,这是一个 隧道事件
,它从根到其发起者进行隧道传输.所以 button 会先获取这个事件,然后再获取 textBlock.
请参考下面的快照以获得清晰的图片:
<小时>更新
仅在按钮上挂钩 PreviewMouseDown
事件并从单个 textBlock 中删除处理程序.检查 e.OrignialSource
以查看 TextBlock
是否是实际的原始来源或按钮.
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e){if (!(e.OriginalSource is TextBlock)){MessageBox.Show("你点击了按钮");}别的{switch ((e.OriginalSource as TextBlock).Text){案例第一":MessageBox.Show("你先点击");休息;案例第二":MessageBox.Show("你点击第二个");休息;案例第三":MessageBox.Show("你点击第三个");休息;}}}
XAML
I newly in WPF and when I learn the material I faced with strange issue.
I build a button, contain layers with text block and I want to recognize where the user click on the button itself, on 'first', 'second, or 'third' (I output a message).
All works fine except the button not raise an event when the user clicks with left button (just with middle or right button).
so my question: Why I not received a message box when I press on the button itself with left mouse button (and I receive the msg 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>
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)
{
// This event not working good
// only middle & right mouse buttons are recognized
MessageBox.Show("You click on the button");
}
Thank you!
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:
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>
这篇关于wpf:无法识别左键单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!