简体   繁体   English

允许点击和点击

[英]Allow clicks and click-through's

I have a Grid over a Button (They are both directly on another Grid, but the Grid is "above").我在Button有一个Grid (它们都直接在另一个网格上,但Grid在“上方”)。 I need to subscribe to certain events in the Grid , but still be able to click on the Button .我需要订阅Grid某些事件,但仍然可以单击Button I set the Background of the Grid to Transparent , to get it to raise events, but then the Button isn't clickable.我将GridBackground设置为Transparent ,以使其引发事件,但是该Button不可点击。 Is there any way to leave IsHitTestVisible = true but let the click go to the next element as well?有没有办法让IsHitTestVisible = true但让点击转到下一个元素?

I'd first look at whether you can put the button above the grid.我首先看看你是否可以将按钮放在网格上方。 You can just move it in the XAML - the further down an element declaration is in the XAML, the higher the z-order.您可以在 XAML 中移动它 - 元素声明在 XAML 中越往下,z 顺序越高。

If you definitely cannot do this, you could override the UI element MouseUp or MouseDown methods and control the setting of the handled property to allow the subsequent elements in the tree to take the clicks.如果您绝对不能这样做,您可以覆盖 UI 元素的 MouseUp 或 MouseDown 方法并控制处理属性的设置,以允许树中的后续元素进行点击。

If you only wanted to restrict this to a specific control, you could inspect the "OriginalSource" property (since all these event args inherit from RoutedEventArgs) to see what the source of the click was and act accordingly:如果您只想将其限制为特定控件,您可以检查“OriginalSource”属性(因为所有这些事件参数都继承自 RoutedEventArgs)以查看点击来源并采取相应措施:

protected override void OnMouseDown(MouseButtonEventArgs e)
{
    var grid = e.OriginalSource as Grid;
    if (grid != null && grid.Tag = "yourgridname") e.Handled = false;
}

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

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