鼠标按下事件未触发(冒泡事件)

发布于 2024-10-08 18:04:19 字数 2020 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难得心□动 2024-10-15 18:04:19

两点:

1) MouseDown="Window_MouseUp" 是否适用于任何地方?

2) 为什么不使用 ClickMode="Press" 而不是 MouseDown 来注册 Click 事件。我不认为 Button 提供/引发 MouseDown 除非可以使用自定义模板。

例子:

<Button Grid.Row="3"
        Margin="5"
        Name="cmd_Clear"
        ClickMode="Press"
        Click="Cmd_Clear_MouseDown">Clear</Button>

Two points:

1) Is MouseDown="Window_MouseUp" everywhere intended?

2) Why not register to Click event with ClickMode="Press" instead of MouseDown. I don't think Button provides/raises MouseDown unless may be with a custom template.

Example:

<Button Grid.Row="3"
        Margin="5"
        Name="cmd_Clear"
        ClickMode="Press"
        Click="Cmd_Clear_MouseDown">Clear</Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文