C# 防止父级在子级鼠标向下移动时触发事件
我在C# 防止父级在子级鼠标向下移动时触发事件,c#,wpf,xaml,C#,Wpf,Xaml,我在窗口的顶部放置了一个网格,其中有一个图像。网格可能正在拖动带有MouseDown事件的窗口 但是,每当我想对子图像触发MouseDown事件时,它都不起作用,而是触发网格的 private void toggleTbr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } private void leapTcb_MouseLeftButtonDown(object send
窗口的顶部放置了一个网格
,其中有一个图像
。网格
可能正在拖动带有MouseDown
事件的窗口
但是,每当我想对子图像触发MouseDown
事件时,它都不起作用,而是触发网格的
private void toggleTbr_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void leapTcb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
//my code
}
如你所见,我尝试了e.Handled=true
但是它没有改变任何东西,然后我想尝试使用PreviewMouseLeftButtonDown
而不是MouseLeftButtonDown
但是,还是一样
我在这里做错了什么,或者如何防止网格
触发
XAML:
图像元素没有背景属性,而是直接在图像元素上设置源属性
然而,您使其运行,但通过指定的更改,它的行为与您想要的一样,至少在我的机器上。使用此功能这是我这边的工作
<Grid x:Name="toggleTbr" MouseLeftButtonDown="toggleTbr_MouseLeftButtonDown" Background="Red">
<Grid x:Name="leapTcb_" Height="21" Width="26">
<Image x:Name="leapTcb" PreviewMouseLeftButtonDown="leapTcb_MouseLeftButtonDown">
<Image.Background>
<ImageBrush Source="Resources/leap_1.png"/>
</Image.Background>
</Image>
</Grid>
</Grid>
您的代码工作正常,但如果不够,您可以尝试使用以下方法:
IsHitTestVisible="True"
哪个事件是从子映像引发的?@SankarannLeapTcb_MouseLeftButtonDown
@RajeevRanjan检查UpdateEyes,实际上我也做了更改以获得更好的性能。
IsHitTestVisible="True"