如何在 WPF 中创建透明的用户控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12824154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a transparent user control in WPF?
提问by Ryan O'Neill
I have a WPF app with an image over which is laid a user control. I would like the user control background to be transparent but the buttons etc on that user control to be solid.
我有一个 WPF 应用程序,上面有一个图像,上面放置了一个用户控件。我希望用户控件背景是透明的,但该用户控件上的按钮等是实心的。
The image below shows what I have now. I would like to have the inside of the red area as transparent (the user control can in theory be many different shapes) but with the controls contained within it, solid.
下图显示了我现在所拥有的。我想让红色区域的内部透明(理论上用户控件可以是许多不同的形状),但其中包含的控件是实心的。


The main window XAML is;
主窗口 XAML 是;
<Canvas Height="450" Width="300">
<Border Opacity=".2" >
<Image Source="D:\BarbourCoat.jpg" Width="300" ></Image>
</Border>
<local:UserControl1 Height="100" Canvas.Left="10" Canvas.Top="10" Width="100"/>
</Canvas>
Whilst the user control XAML is;
虽然用户控件 XAML 是;
<Canvas Background="Transparent">
<Path Data="M 10 10 L 100 10 L 250 50 L 280 200 L 180 250 L 25 270 Z" Stroke="Red"></Path>
<Button Content="Button" HorizontalAlignment="Left" Margin="41,53,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="90,112,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBlock HorizontalAlignment="Left" Margin="69,209,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
</Canvas>
I'm having a hard time getting my head round this, any help would be greatly appreciated.
我很难理解这一点,任何帮助将不胜感激。
回答by BenMaddox
In XAML, have you made the whole UserControl transparent?
在 XAML 中,您是否使整个 UserControl 透明?
<UserControl
Background="Transparent">
<UserControl
Background="Transparent">

