wpf - 如何处理点击事件然后传递给WPF中的底层应用程序
问题描述
我有一个 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,点击也不会冒泡到底层应用程序。
这可能吗?
解决方案
在您的单击事件处理程序方法中,通过执行所需的代码“处理”它之后,只需设置:
e.Handled = false;
这告诉隧道/冒泡事件它认为您尚未处理此点击,并将允许其他点击事件执行。
推荐阅读
- stripe-payments - 使用有效的付款方式重试后,Stripe 订阅发票保持打开状态
- visual-studio-code - 在 VS Code 中选择文本并单击搜索按钮不再将所选文本粘贴到搜索字段中
- reactjs - React.FC 之间的 React TypeScript 区别
和函数() - r - 更改从 Jupyter Lab 导出的 PDF 中的字体大小和行距/高度
- python - 如何仅选择从空间实体中提取的第一个实体?
- proxy - 是否可以在 OTA 更新期间“捕获/拦截”并下载库存固件文件?
- python - ValueError:第 440902 行上的无效向量 | 使用 gensim.models.keyedvectors.word2vec() 函数加载 wiki.ar.vec
- c# - c#处理访问冲突异常并防止app终止
- python - Django - Python - 如何使用 JsonResponse 在响应中包含状态码?
- java - 如何设置值 SoapRequestCreator