Search code examples
wpfopacitymaskwindowsformshost

OpacityMask not working with WPF WindowsFormsHost?


I'm trying to put a rounded border on a control which is hosted through a WindowsFormHost object. It seems like no matter what I set for OpacityMask, it has no effect on the rendering. Is there something I've missed?

Here is the XAML code I am using. The child control is added at run-time. I've tried various combinations of masks, none of which worked for me. Any help would be appreciated. Thanks!

    <WindowsFormsHost Background="#FF2BBA62" Height="414" Width="516" Margin="176.5,223,309.5,92" Name="vcxHost1" UseLayoutRounding="False" ClipToBounds="True" >
        <WindowsFormsHost.OpacityMask>
            <VisualBrush>
                <VisualBrush.Visual>
                    <Rectangle Height="10" Width="100" Name="border1" />
                </VisualBrush.Visual>
            </VisualBrush>
        </WindowsFormsHost.OpacityMask>
    </WindowsFormsHost>

Solution

  • I suspect it doesn't work for the same reason that the z-indexing doesn't work. From the linked doc: "A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of WPF elements."

    See the paragraph on Layout Limitations ... http://msdn.microsoft.com/en-us/library/ms744952.aspx

    This may also be helpful ... http://msdn.microsoft.com/en-us/library/ms742522.aspx