Search code examples
xamlwindows-store-appsuwp

Let background catch Tapped events?


I have a grid with a semi transparent background, and I'd like the user to be able to tap the background to close the popup.

Now I have this XAML:

<Grid
    Tapped="Close_Tapped">
    <Grid.Background>
        <SolidColorBrush Color="Black" Opacity=".75"/>
    </Grid.Background>
    <!-- Inner border -->
    <Border
        Background="White"
        CornerRadius="5">
        <!-- Other items -->
    </Border>
</Grid>

And the Tapped event should only be applied to the Grid background, but obviously this applies it to the entire grid.

Any ideas to have this Tapped event only work on the background?


Solution

  • You can either check if e.OriginalSource in the Close_Tapped event handler is aGrid or add another element to the Grid, below other elements, which will become your background and then move Tapped handler there

    <Grid>
        <Border Tapped="Close_Tapped">
            <Border.Background>
                <SolidColorBrush Color="Black" Opacity=".75"/>
            </Border.Background>
        </Border>
        <!-- Inner border -->
        <Border
            Background="White"
            CornerRadius="5">
            <!-- Other items -->
        </Border>
    </Grid>