透明系统颜色渐变(WPF)

时间:2011-01-14 17:28:13

标签: wpf xaml transparent gradient

如何在XAML中实现这样的效果:

    <LinearGradientBrush x:Key="BrightSeparatorGradient" StartPoint="0.0, 0.5" EndPoint="1.0, 0.5">
        <GradientStop Offset="0.0" Color="{StaticResource {x:Static SystemColors.ControlLightColorKey}}" /> <!-- But fully transparent -->
        <GradientStop Offset="0.5" Color="{StaticResource {x:Static SystemColors.ControlLightColorKey}}" />
        <GradientStop Offset="1.0" Color="{StaticResource {x:Static SystemColors.ControlLightColorKey}}" /> <!-- But fully transparent -->
    </LinearGradientBrush>

我试图创建两个分别具有适当颜色和不透明度设置的画笔,分别为0.0和1.0,但是编译器拒绝将画笔作为一种颜色(这是非常逻辑的,在第二个想法上:))。 / p>

致敬 - Spook。

2 个答案:

答案 0 :(得分:6)

我会创建一个MarkupExtension来获取Color并返回具有指定不透明度的Color

public class OpacityExtension : MarkupExtension
{
    private readonly Color color;
    public byte Opacity { get; set; } // defaults to 0, so you don't have 
                                      // to set it for the color to be transparent

    public OpacityExtension(Color color)
    {
        this.color = color;           
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Color.FromArgb(Opacity, color.R, color.G, color.B);
    }
}

然后你就这样使用它:

<LinearGradientBrush x:Key="BrightSeparatorGradient" StartPoint="0.0, 0.5" EndPoint="1.0, 0.5">
    <GradientStop Offset="0" Color="{lcl:Opacity {StaticResource {x:Static SystemColors.ControlLightColorKey}}}" />
    <GradientStop Offset=".5" Color="{StaticResource {x:Static SystemColors.ControlLightColorKey}}" />
    <GradientStop Offset="1" Color="{lcl:Opacity {StaticResource {x:Static SystemColors.ControlLightColorKey}}}" />
</LinearGradientBrush>

答案 1 :(得分:0)

幽灵,

为什么以下内容对您不起作用?

<LinearGradientBrush x:Key="BrightSeparatorGradient" StartPoint="0.0, 0.5" EndPoint="1.0, 0.5">
    <GradientStop Offset="0.0" Color="Transparent" /> 
    <GradientStop Offset="0.5" Color="{StaticResource {x:Static SystemColors.ControlLightColorKey}}" />
    <GradientStop Offset="1.0" Color="Transparent" /> 
 </LinearGradientBrush>