.net 在 WPF 中创建透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2557862/
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
Creating transparent background in WPF
提问by codematrix
I have a button with a backgroun image of color white. My button is sitting on the toolbar which has a bacground color of Blue. When the button is sitting on the toolbar, the button looks white, however I want it to look like blue as the the background color of toolbar is Blue.
我有一个带有白色背景图像的按钮。我的按钮位于工具栏上,其背景颜色为蓝色。当按钮位于工具栏上时,按钮看起来是白色的,但是我希望它看起来像蓝色,因为工具栏的背景颜色是蓝色。
How should I achieve this in WPF.. Please let me know.
我应该如何在 WPF 中实现这一点。请告诉我。
回答by oltman
Are you sure you want to use an image? If the image is just a solid color, you're probably better off just setting the button's background to white and adjusting its opacity. Like:
您确定要使用图像吗?如果图像只是纯色,您最好将按钮的背景设置为白色并调整其不透明度。喜欢:
<Button Background="White" Opacity="0.5" />
Where Opacity can be any value between 0 and 1.
其中 Opacity 可以是 0 到 1 之间的任何值。
EDIT: I'm not sure what you mean by "shades." Are you aware that you can set the background of a Button to a gradient? You'll want to change the colors I'm sure, but it will look something like this:
编辑:我不确定你所说的“阴影”是什么意思。您知道可以将 Button 的背景设置为渐变吗?我敢肯定,您会想要更改颜色,但它看起来像这样:
<Button>
<Button.Background>
<LinearGradientBrush>
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Orange" Offset="0.5" />
<GradientStop Color="Red" Offset="1.0" />
</LinearGradientBrush>
<Button.Background>
</Button>
回答by kiwipom
You can use the System.Windows.Media.ColorTransparent, like this:
您可以使用System.Windows.Media.ColorTransparent, 像这样:
<Button Background="Transparent" />
回答by Simon Buchan
You have to make sure your image has transparency, WPF won't try to figure out what parts in your image should be see-through and what parts should be opaque. Check that your image editor can export images with transparency, most likely to PNG.
您必须确保您的图像具有透明度,WPF 不会试图弄清楚图像中的哪些部分应该是透明的,哪些部分应该是不透明的。检查您的图像编辑器是否可以导出具有透明度的图像,最有可能导出为 PNG。

