wpf 在 XAML 中指定命名颜色的透明度级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15114651/
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
Specify transparency level of a named color in XAML
提问by dotNET
Is there a way in XAML to create a color object from a named color with different custom transparency level? e.g.
XAML 中有没有办法从具有不同自定义透明度级别的命名颜色创建颜色对象?例如
<Label Background="{SkyBlue;220}" />
I know this doesn't work, but just wanted to quote an example.
我知道这行不通,但只是想举个例子。
回答by dotNET
One of those times when you find the answer yourself. Here's the correct way for any future reader:
你自己找到答案的时候之一。这是任何未来读者的正确方法:
<Label.Background>
<SolidColorBrush Color="SkyBlue" Opacity=".9" />
</Label.Background>
Opacity
ranges between 0 and 1, 1 being full opaque (non-transparent).
Opacity
范围在 0 和 1 之间,1 表示完全不透明(不透明)。
Edit
编辑
Regarding @Dai's comment, this method indeed doesn't reset or override the transparency level of the specified color in case you're referencing a color resource that already has set some transparency. For example if your resource color is SkyBlue
with transparency set to 0.5, and now you want to set it to 0.7 instead, the above method won't work directly.
关于@Dai 的评论,此方法确实不会重置或覆盖指定颜色的透明度级别,以防您引用已经设置了一些透明度的颜色资源。例如,如果您的资源颜色的SkyBlue
透明度设置为 0.5,而现在您想将其设置为 0.7,则上述方法将无法直接工作。
To handle that situation, all you need to do is to create a little Converter
that resets the alpha component of the input color. Something like this:
为了处理这种情况,您需要做的就是创建一个Converter
重置输入颜色的 alpha 分量的小工具。像这样的东西:
public class NoTransparencyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var C = ((Color)value);
return Color.FromArgb(0xFF, C.R, C.G, C.B);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
and then use it in your XAML:
然后在您的 XAML 中使用它:
<SolidColorBrush Color="{Binding Path="YOUR_COLOR_RESOURCE" Converter={x:Static NoTransparencyConverter}}" Opacity=".9" />
回答by Chris W.
Just to add a little to this one. Yes, you can absolutely set the Opacity
via declarative as you showed in your answer but you don't even need the Dependency Property set, you can do it straight in the hex for the color by utilizing the Alpha
on top of your RGB
values (which by the way offers better performance than opacity
). Where as, say you have the value for Blackand you want to give it some opacity. The first 2 octets of that value can be added to accomplish the same thing.
只是为了增加一点。是的,您绝对可以Opacity
像您在答案中显示的那样设置via 声明,但您甚至不需要 Dependency Property 集,您可以通过利用值的Alpha
顶部RGB
(由方式提供比opacity
)更好的性能。比如,假设你有黑色的价值,你想给它一些不透明度。可以添加该值的前 2 个八位字节来完成相同的事情。
As example;
例如;
"#000000" = Black (SOLID)
“#000000” = 黑色(纯色)
where as;
然而;
"#33000000" = Black (With 20% Opacity)
"#33000000" = 黑色(不透明度为 20%)
"#77000000" = Black (With 47% Opacity)
"#77000000" = 黑色(不透明度为 47%)
"#E5000000" = Black (With 90% Opacity)
"#E5000000" = 黑色(90% 不透明度)
Just to throw some elaboration out there and potentially help you in the future when the Opacity
property may not be so readily available or is set as immutable. Hope this helps.
只是为了在那里进行一些详细说明,并在将来当该Opacity
属性可能不那么容易获得或设置为不可变时可能为您提供帮助。希望这可以帮助。
ADDENDUM:It's also worth noting that a setting to a property like alpha will always be more performant than setting an opacity to an entire element. This also applies to HTML/CSS scenarios where in something like background-color: rgba(0,0,0,.5)
is better practice than say a <div style="background: black;opacity: .5)
附录:还值得注意的是,设置像 alpha 这样的属性总是比设置整个元素的不透明度更高效。这也适用于 HTML/CSS 场景,其中background-color: rgba(0,0,0,.5)
比说一个更好的实践<div style="background: black;opacity: .5)