0

I have a WPF window where the background is {x:Null} and AllowTransparency=True

<Window x:Class="ClickThrough"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SeeThru" Height="200" Width="200"
        Topmost="True"
        WindowStyle="None" 
        AllowsTransparency="True"
        Background="{x:Null}">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="80"/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <Border Background="CadetBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                CornerRadius="5,5,0,0" Margin="-1,0,-1,0" MouseLeftButtonDown="DragWindow">
        </Border>
        <Grid MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp" Grid.Row="1"></Grid>
    </Grid>
</Window>

I can click on the 'window' and the click passes through the window and the underlying application receives it. This is perfect. However I also want to be able to handle the click event and then allow it to pass-through to the underlying application.

The event handler on the inner Grid doesn't register the events.

private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("clicked");
    e.Handled = false;
}

If I change the Background in the inner grid to a color.

<Grid MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp" Grid.Row="1" Background="Aqua">

Now the event is received, however the click does not bubble through to the underlying application even thought the handled is set to false.

Is this possible?

Druzil
  • 127
  • 9

1 Answers1

0

In your click event handler method, after you've "handled" it by executing your desired code, just set:

e.Handled = false;

This tells the tunneling/bubbling events that it thinks you have not handled this click yet, and will allow other click events to execute.

Tam Bui
  • 2,940
  • 2
  • 18
  • 27
  • This doesn't appear to work for the window, as it does not receive the event in the first place. The event only registers when there is a non-transparent/non-null background. – Druzil Aug 30 '21 at 23:03
  • I've updated the question for clarify of this – Druzil Aug 30 '21 at 23:11
  • @Druzil in your updated question, I believe the reason why the click does not bubble through is because you are using `Message.Show`, which changes the focus of the visual/logical tree. Replace it with `Console.WriteLine("Click")` and it should completely pass through the `UIElement_OnMouseLeftButtonUp` method (because you ended the method with `e.Handled = false`), and it will allow other click events to fire after that. – Tam Bui Aug 31 '21 at 17:18
  • Thanks @Tam Bui, unfortunately I tried that and I still can't get it to work. – Druzil Sep 01 '21 at 00:11
  • Have you tried switching to the "Preview" click events? What is the "underlying application" that you want to execute that is not executing when UIElement_MouseLeftButtonUp executes? Perhaps I'm not understanding what you want, and it would be better if you provided a mininum reproducible sample to isolate your problem, because code snippets can potentially hide the source of your issue. – Tam Bui Sep 01 '21 at 16:19