Hey guys,
I am confused. Why does this Rectangle intercept the mouse clicks instead of passing the click to the application underneath the window. The Window is transparent and so is LayoutRoot. Clicking next to the small Rectangle will activate the application underneath. I expected the Rectangle to not react to user clicks and also pass the click to the application underneath, but this doesn’t work. Why not?
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication3.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480"
WindowStyle="None"
AllowsTransparency="True"
Background="{x:Null}">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="White"
Stroke="Black"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="100"
Height="100"
IsHitTestVisible="False" />
</Grid>
</Window>
Cheers,
Laurent
Nope, consider this:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication3.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480"
WindowStyle="None"
AllowsTransparency="True"
Background="{x:Null}">
<Grid x:Name="LayoutRoot" Background="{x:Null}">
<Button Margin="205,162,252,221" Content="Button"/>
<Rectangle Fill="White"
Stroke="Black"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="298"
Height="207"
IsHitTestVisible="False" />
</Grid>
</Window>
The Button will get the clicks through the rectangle, but even the Grid and the Window has no background, clicks don’t get through the window to the app underneath
Puzzled,
Laurent
Nope, consider this:
Thanks guys. This will work to let all clicks go through. In my case I want the button to get the clicks, but not the rectangle. I am at a client and we were trying to find the best way to do a shadowed window (with a blurred black rectangle) where the shadow lets the clicks go through (same behavior than shadowed windows in Win7). Other options we discussed are to use templated windows but the client doesn’t like this option. I think they will either just leave it the way it is and get the clicks (doesn’t seem to be a huge deal for them) or use some Win32 interop to pass the clicks to the window below.
I am thinking of grouping all the Win32 interop code sections in files with a header saying “Abandon all hope, ye who enter here”.
http://en.wikipedia.org/wiki/Inferno_(Dante)
Cheers
Laurent
Jer, I don't think Popups behave the way you would want in this case -- they tend to stay topmost and not move with their parent. Trying to get them to do that is kind of a PITA. Past attempts we've made to make them do what we want usually resulted in more code that we wanted to maintain (along with more Tylenol to get rid of the headache!).
Laurent, you might try using an HwndHook on the window and looking for WM_NCHITTEST. If the mouse is over a clickable area, you return HTBORDER or some other HT* constant (look in Reflector at the Window class because I'm pretty sure they do something like that in there); if it's over the shadow, you return HTNOWHERE. I haven't tried this but it might work.
Hi Erik,
Good call about the Popup, I had that bite me in the past.
I will check the other way you suggest. My answer to the client was that it is not possible without Win32 interop, so it was good enough.
Cheers and thanks all,
Laurent