尝试让 OnMouse 事件出现在子项中FrameworkElement
。父元素是一个Panel
(以及Background
属性不为空)。
class MyFrameworkElement : FrameworkElement
{
protected override void OnMouseDown(MouseButtonEventArgs e)
{
// Trying to get here!
base.OnMouseDown(e);
}
}
public class MyPanel : Panel
{
protected override void OnMouseDown(MouseButtonEventArgs e)
{
// This is OK
base.OnMouseDown(e);
}
}
OnMouse 永远不会被调用,事件总是未处理,Snoop 告诉我,路由事件似乎只到达Panel
元素。
<Window
x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfApplication5"
Title="Window1" Height="300" Width="300">
<Border x:Name="myBorder" Background="Red">
<l:MyPanel x:Name="myPanel" Background="Transparent">
<l:MyFrameworkElement x:Name="myFE"/>
</l:MyPanel>
</Border>
</Window>
文件说FrameworkElement
处理输入,但为什么不在这种情况下呢?
OnMouseDown
仅当您的元素响应命中测试时才会被调用。看视觉层的命中测试 http://msdn.microsoft.com/en-us/library/ms752097.aspx。默认实现将对绘制的图形进行命中测试OnRender http://msdn.microsoft.com/en-us/library/system.windows.uielement.onrender.aspx。创建一个Panel
with a Transparent
背景有效,因为Panel
在其整个区域上绘制一个矩形,该矩形将捕获命中测试。您可以通过覆盖获得相同的效果OnRender
绘制透明矩形:
protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawRectangle(Brushes.Transparent, null,
new Rect(0, 0, RenderSize.Width, RenderSize.Height));
}
你也可以覆盖HitTestCore http://msdn.microsoft.com/en-us/library/system.windows.media.visual.hittestcore.aspx这样所有的点击都算作点击:
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)