如何在 WPF 中制作多向渐变? [英] How do I make a multi-directional gradient in WPF?

查看:65
本文介绍了如何在 WPF 中制作多向渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在 XAML 中设置了如下渐变:

I currently have a gradient set up in XAML as follows:

<Canvas>
    <Canvas.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0"/>
            <GradientStop Color="Red" Offset="0.5"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </Canvas.Background>
</Canvas>

结果:

然而,这不是我想要实现的.我想要的是将一个渐变过渡到纯色.以下是我想要实现的目标:

However, this is not what I want to achieve. What I want is to have one gradient transition to a solid colour. Below is what I am trying to achieve:

从上图可以看出,顶部有一个白红渐变,随着下降,它平滑地过渡到黑色.我还没有找到一种方法来做到这一点,这就是我需要帮助的地方.有没有办法做到这一点?如果有,实现这一目标(您知道的)的最佳方法是什么?

As one can see from the above image, there is a white-red gradient at the top, and that transitions smoothly to black as it goes down. I have yet to find a way to do this, and this is where I need help. Is there a way to do this? If there is, what is the best way to achieve this (that you know of)?

推荐答案

您可以使用改变不透明度的垂直 LinearGradientBrush 覆盖第二个元素:

You may overlay a second element with a perpendicular LinearGradientBrush that changes opacity:

<Grid>
    <Canvas>
        <Canvas.Background>
            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                <GradientStop Color="White" Offset="0"/>
                <GradientStop Color="Red" Offset="1"/>
            </LinearGradientBrush>
        </Canvas.Background>
    </Canvas>
    <Canvas>
        <Canvas.Background>
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#00000000" Offset="0"/>
                <GradientStop Color="Black" Offset="1"/>
            </LinearGradientBrush>
        </Canvas.Background>
    </Canvas>
</Grid>

这篇关于如何在 WPF 中制作多向渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆