简体   繁体   English

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

[英]Mouse Down Event not firing (Bubbling events)

I am a novice to WPF. 我是WPF的新手。 I started learning about RoutedEvents in WPF. 我开始学习WPF中的RoutedEvents。 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. 按钮的mouseDown事件的处理程序在树层次结构中与其他处理程序不同。 The event is not firing.. 该事件未触发。

But if i add in the .cs file the following code 但是如果我在.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);

the Cmd_Clear_MouseDown event is fired and the event is bubbled up to the grid and the grid fires Window_MouseUp. Cmd_Clear_MouseDown事件被触发,并且该事件冒泡到网格,并且网格触发Window_MouseUp。

Two points: 两点:

1) Is MouseDown="Window_MouseUp" everywhere intended? 1)是否在各处都有MouseDown="Window_MouseUp"

2) Why not register to Click event with ClickMode="Press" instead of MouseDown . 2)为什么不使用ClickMode="Press"而不是MouseDown注册Click事件。 I don't think Button provides/raises MouseDown unless may be with a custom template. 我不认为Button可以提供/提高MouseDown除非可以使用自定义模板。

Example: 例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM