Search code examples
c#wpfxaml

How can I check if user DIDN'T click on any UI Elements? If he missed the click and simply clicked on the background


So I modify some button's colors that I want to reset when the user clicks outside any of the buttons from the grid, but still clicks somewhere in the UserControl. I tried to add a MouseDown event to the background of the grid but it it not a valid event. Something like this

<Grid.Background>
        <ImageBrush
            Mouse.MouseDown="ImageBrush_MouseDown"
            ImageSource="/Res/maptest3.png"
            Stretch="None"
            />
 </Grid.Background>

Solution

  • <Window x:Class="MyApp.MainWindow" x:Name="windowClicked" //Add this to the xaml file

    In the source code:

    private void windowClicked_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // or your what you want to do
            MessageBox.Show("Background Clicked"); 
        }