Click here to Skip to main content
15,918,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following XAML sample that should raise a PreviewMouseDown (same for MouseDown) event when clicking on the Grid background:

C#
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid PreviewMouseDown="Grid_PreviewMouseDown"
          HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch"
          >
        <Button Margin="12,12,313,185">
            Test 23
        </Button>
    </Grid>
</Window>


When clicking onto the button, the event gets fired, but not when I click onto the grid background.

Funny enough, when I add a background color to the grid, the event will fire on a click in the background (even when I set the color to transparent):

<Grid PreviewMouseDown="Grid_PreviewMouseDown" 
    HorizontalAlignment="Stretch" 
    VerticalAlignment="Stretch"
    Background="Transparent"
    >


Is there a good reason for this behavior or is it the usual madness?

Many thanks for your help!
Posted
Comments
Sergey Alexandrovich Kryukov 23-Sep-11 3:02am    
Do you think madness is usual? With WPF or with .NET? I can imagine your life... :-)
--SA
Doc Lobster 23-Sep-11 4:21am    
I suppose there is a certain degree of madness in most situations we have to cope with. Usual madness is okay by my standards, its something you can (or have to) hook off and get on with. It can be found anywhere, e.g. in a choleric co-worker. There is also unusual madness and sanity, I actually don't know which one of them is more threatening ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900