0

我有一个 WPF 窗口,其中背景是 {x:Null} 和 AllowTransparency=True

<Window x:Class="ClickThrough"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SeeThru" Height="200" Width="200"
        Topmost="True"
        WindowStyle="None" 
        AllowsTransparency="True"
        Background="{x:Null}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="80"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Border Background="CadetBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                CornerRadius="5,5,0,0" Margin="-1,0,-1,0" MouseLeftButtonDown="DragWindow">
        </Border>
        <Grid MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp" Grid.Row="1"></Grid>
    </Grid>
</Window>

我可以点击“窗口”,点击通过窗口,底层应用程序接收它。太棒了。但是,我也希望能够处理单击事件,然后允许它传递到底层应用程序。

内部 Grid 上的事件处理程序不注册事件。

private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("clicked");
    e.Handled = false;
}

如果我将内部网格中的背景更改为一种颜色。

<Grid MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp" Grid.Row="1" Background="Aqua">

现在接收到事件,但是即使将handled 设置为false,点击也不会冒泡到底层应用程序。

这可能吗?

4

1 回答 1

0

在您的单击事件处理程序方法中,通过执行所需的代码“处理”它之后,只需设置:

e.Handled = false;

这告诉隧道/冒泡事件它认为您尚未处理此点击,并将允许其他点击事件执行。

于 2021-08-30T17:10:13.033 回答