简体   繁体   中英

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"). I need to subscribe to certain events in the Grid , but still be able to click on the Button . I set the Background of the Grid to Transparent , to get it to raise events, but then the Button isn't clickable. Is there any way to leave IsHitTestVisible = true but let the click go to the next element as well?

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.

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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