문제

I have two controls A and B. Control B is placed over A. When B is clicked only it's clicked event is fired, however I need the click event of A to fire too. Can this somehow be realized using routed events?

Here's a screenshot of the controls:

alt text http://www.imagebanana.com/img/hu5znv4/buttons.png

Of course this case is just a reduced example, I'm looking for a general way to raise events on all controls below the mouse cursor / at one location. Hoewever, a concrete solution to this problem would help me too! Thank you very much for any hint!

도움이 되었습니까?

해결책

This can happen if the event handler of the inner control sets the e.Handled property to true. This would prevent the routed event from bubbling any further. But I just checked and I do not see that behavior in WPF 4.

If e.Handled is preventing your event, what you can do however is use the UIElement.AddHandler overload that takes a boolean handledEventsToo parameter to hook up your click event. This has to be done in code, not markup. Then you should receive the Click event in the outer control and the e.Handled property will already be set to true.

The following code does set the background of both buttons when I click the inner button.

<Grid>
    <Button VerticalAlignment="Center" HorizontalAlignment="Center" Click="Click1">
        <Button Width="100" Height="25" Margin="20" Click="Click2" />
    </Button>
</Grid>

Code Behind

private void Click1( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Blue );
}

private void Click2( object sender, RoutedEventArgs e )
{
    ( (Button)sender ).Background = new SolidColorBrush( Colors.Red );
    //e.Handled = true; // uncomment to stop bubbling
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top