鼠标按下事件未触发(冒泡事件)
我是 WPF 的新手。我开始学习 WPF 中的 RoutedEvents。我尝试了一个示例,但遇到了一个问题:
<Grid Margin="5" Name="Grid" MouseDown="Window_MouseUp">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Label BorderBrush="Black" BorderThickness="1" Grid.Row="0" Margin="5" Name="FancyLabel" MouseDown="Window_MouseUp" >
<StackPanel Name="Stack" MouseDown="Window_MouseUp">
<TextBlock Margin="3" Name="txtBlock1">
Click Any Where
</TextBlock>
<TextBlock Margin="50" Name="txtBlock2" >
Click me also
</TextBlock>
</StackPanel>
</Label>
<ListBox Grid.Row="1" Margin="5" Name="ListMessages"/>
<Button Grid.Row="3" Margin="5" Name="cmd_Clear" MouseDown="Cmd_Clear_MouseDown" >Clear</Button>
</Grid>
按钮的 mouseDown 事件的处理程序与树层次结构中的其他事件不同。该事件没有触发。
但是,如果我在 .cs 文件中添加以下代码,
Grid.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp),true);
Stack.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
FancyLabel.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
txtBlock1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
txtBlock2.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
Img1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
cmd_Clear.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Cmd_Clear_MouseDown), true);
则会触发 Cmd_Clear_MouseDown 事件,并且该事件会向上冒泡到网格,并且网格会触发 Window_MouseUp。
I am a novice to WPF. I started learning about RoutedEvents in WPF. I tried a sample and i met up with a problem
<Grid Margin="5" Name="Grid" MouseDown="Window_MouseUp">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Label BorderBrush="Black" BorderThickness="1" Grid.Row="0" Margin="5" Name="FancyLabel" MouseDown="Window_MouseUp" >
<StackPanel Name="Stack" MouseDown="Window_MouseUp">
<TextBlock Margin="3" Name="txtBlock1">
Click Any Where
</TextBlock>
<TextBlock Margin="50" Name="txtBlock2" >
Click me also
</TextBlock>
</StackPanel>
</Label>
<ListBox Grid.Row="1" Margin="5" Name="ListMessages"/>
<Button Grid.Row="3" Margin="5" Name="cmd_Clear" MouseDown="Cmd_Clear_MouseDown" >Clear</Button>
</Grid>
The handler for the mouseDown event of the button is different from others int the tree hierarchy. The event is not firing..
But if i add in the .cs file the following code
Grid.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp),true);
Stack.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
FancyLabel.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
txtBlock1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
txtBlock2.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
Img1.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Window_MouseUp), true);
cmd_Clear.AddHandler(UIElement.MouseDownEvent, new MouseButtonEventHandler(Cmd_Clear_MouseDown), true);
the Cmd_Clear_MouseDown event is fired and the event is bubbled up to the grid and the grid fires Window_MouseUp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两点:
1)
MouseDown="Window_MouseUp"
是否适用于任何地方?2) 为什么不使用
ClickMode="Press"
而不是MouseDown
来注册Click
事件。我不认为Button
提供/引发MouseDown
除非可以使用自定义模板。例子:
Two points:
1) Is
MouseDown="Window_MouseUp"
everywhere intended?2) Why not register to
Click
event withClickMode="Press"
instead ofMouseDown
. I don't thinkButton
provides/raisesMouseDown
unless may be with a custom template.Example: