c#WPF对Winform控件的透明度

时间:2009-06-01 20:46:10

标签: c# wpf winforms controls transparency

我有一个WPF控件,我想覆盖到WinForms应用程序。所以我尽职尽责地创建了一个可以显示以下WPF对象的元素主机:

<UserControl x:Class="LightBoxTest.LightBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300" Background="Transparent">
    <Grid Name="dialogHolder" Background="Transparent" Opacity="1">
        <Rectangle Name="rectangle1" Stroke="White" Fill="Black" RadiusX="10" RadiusY="10" Opacity="0.5" />
        <StackPanel Name="stackPanel1" Background="Transparent" Height="300" VerticalAlignment="Top">
            <Rectangle Name="spacer" Opacity="0" Stroke="Gray" Fill="White" RadiusX="10" RadiusY="10" Height="100" Width="300" />
            <Grid Height="100" Name="contentHolder" Width="250">
                <Rectangle Name="dialog" Stroke="Gray" Fill="White" RadiusX="10" RadiusY="10" Height="100" Width="250" />
            </Grid>
        </StackPanel>
    </Grid>
</UserControl>

麻烦的是,WinForm表单上的控件不呈现,WPF只是在屏幕上删除它们。

元素主机的创建方式如下:

dialogHost = new ElementHost(); 
dialogHost.Child = dialog;
dialogHost.BackColorTransparent = true;
dialogHost.BringToFront();  
dialogHost.Show();

我应该做些什么吗?我不是吗?

是否存在关于在Winforms上显示透明WPF控件的已知问题?

任何可能有用的文章?

注意:此问题与this question

有关

4 个答案:

答案 0 :(得分:7)

我认为你遇到了airspace issue。 AFAIK,你不能混合WPF透明度和ElementHost透明度,因为ElementHost拥有空域。

关于创建非矩形hwnds来托管WPF内容的链接中有一个简短的模糊,这可能会让你更远。

也许你可以考虑将更多的WinForms应用程序迁移到WPF?

答案 1 :(得分:2)

你应该读到这个:Black background before loading a wpf controll when using ElementHost 只是隐藏和展示它(不酷但有效)

答案 2 :(得分:0)

您可能已经尝试过这个,但是如何在用户控件上设置不透明度?

答案 3 :(得分:0)

这似乎是互操作airspace问题。