Thanks to the comment made by @NETscape I tried to get a hold of the actual Window
for the Popup
. It works with the following code (after the Popup
is initially shown I might add..)
// FromVisual can take any Visual inside the Popup..
HwndSource popupHwndSource = HwndSource.FromVisual(rectangle) as HwndSource;
Combining this with the answer to this question I created an attached behavior which adds the property IsPopupEventTransparent
which can be set on any child in the Popup
. At first, this solution caused a bug which forced the users to click twice to re-activate the Window
after the Popup
lost its event transparency. I solved this with a "Mouse.Capture
-workaround".
elementInPopup.Dispatcher.BeginInvoke(new Action(() =>
{
Keyboard.Focus(elementInPopup);
Mouse.Capture(elementInPopup);
elementInPopup.ReleaseMouseCapture();
}));
Usable like this
<Popup AllowsTransparency="True"
...>
<Border inf:PopupBehavior.IsPopupEventTransparent="{Binding SomeProperty}"
BorderThickness="1"
BorderBrush="Black"
Background="White"
Margin="0 0 8 8">
<Border.Effect>
<DropShadowEffect ShadowDepth="2.25"
Color="Black"
Opacity="0.4"
Direction="315"
BlurRadius="4"/>
</Border.Effect>
<!--...-->
</Border>
</Popup>
PopupBehavior
public class PopupBehavior
{
public static readonly DependencyProperty IsPopupEventTransparentProperty =
DependencyProperty.RegisterAttached("IsPopupEventTransparent",
typeof(bool),
typeof(PopupBehavior),
new UIPropertyMetadata(false, OnIsPopupEventTransparentPropertyChanged));
public static bool GetIsPopupEventTransparent(DependencyObject obj)
{
return (bool)obj.GetValue(IsPopupEventTransparentProperty);
}
public static void SetIsPopupEventTransparent(DependencyObject obj, bool value)
{
obj.SetValue(IsPopupEventTransparentProperty, value);
}
private static void OnIsPopupEventTransparentPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = target as FrameworkElement;
if ((bool)e.NewValue == true)
{
FrameworkElement topParent = VisualTreeHelpers.GetTopParent(element) as FrameworkElement;
topParent.Opacity = 0.4;
HwndSource popupHwndSource = HwndSource.FromVisual(element) as HwndSource;
WindowHelper.SetWindowExTransparent(popupHwndSource.Handle);
}
else
{
FrameworkElement topParent = VisualTreeHelpers.GetTopParent(element) as FrameworkElement;
topParent.Opacity = 1.0;
HwndSource popupHwndSource = HwndSource.FromVisual(element) as HwndSource;
WindowHelper.UndoWindowExTransparent(popupHwndSource.Handle, element);
}
}
}
WindowHelper based on the answer to this this question
public static class WindowHelper
{
private static Dictionary<IntPtr, int> _extendedStyleHwndDictionary = new Dictionary<IntPtr, int>();
const int WS_EX_TRANSPARENT = 0x00000020;
const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
public static void SetWindowExTransparent(IntPtr hwnd)
{
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
if (_extendedStyleHwndDictionary.Keys.Contains(hwnd) == false)
{
_extendedStyleHwndDictionary.Add(hwnd, extendedStyle);
}
}
public static void UndoWindowExTransparent(IntPtr hwnd, FrameworkElement elementInPopup)
{
if (_extendedStyleHwndDictionary.Keys.Contains(hwnd) == true)
{
int extendedStyle = _extendedStyleHwndDictionary[hwnd];
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle);
// Fix Focus problems that forces the users to click twice to
// re-activate the window after the Popup loses event transparency
elementInPopup.Dispatcher.BeginInvoke(new Action(() =>
{
Keyboard.Focus(elementInPopup);
Mouse.Capture(elementInPopup);
elementInPopup.ReleaseMouseCapture();
}));
}
}
}