XAML Gradient as Button Background

PK_112

New member
Joined
Feb 11, 2025
Messages
3
Programming Experience
5-10
Good morning, I'm not quite sure if I'm in the right place with this question, if not please let me know.

I'm just starting out with WPF and have already hit my first hurdle.
I would like to set a colour gradient as the button background. I've managed it for simple rectangles, but somehow it doesn't work for buttons... Can anyone help me?


The following code works for rectangles

C#:
<Rectangle>
    <Rectangle.Fill>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
            <GradientStop Color="Yellow" Offset="0.0" />
            <GradientStop Color="Red" Offset="0.25" />
            <GradientStop Color="Blue" Offset="0.75" />
            <GradientStop Color="LimeGreen" Offset="1.0" />
        </LinearGradientBrush>
    </Rectangle.Fill>
</Rectangle>

Thanks a lot, Philipp
 
Read about ControlTemplate
 
I BELIEVE THIS IS THE PROPER WAY TO MAKE THAT HAPPEN.

Setting a Color Gradient as Button Background in WPF:
<Button Width="200" Height="50">
    <Button.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
            <GradientStop Color="Red" Offset="0.0" />
            <GradientStop Color="Blue" Offset="1.0" />
        </LinearGradientBrush>
    </Button.Background>
    Click Me
</Button>
 
I BELIEVE THIS IS THE PROPER WAY TO MAKE THAT HAPPEN.

Setting a Color Gradient as Button Background in WPF:
<Button Width="200" Height="50">
    <Button.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
            <GradientStop Color="Red" Offset="0.0" />
            <GradientStop Color="Blue" Offset="1.0" />
        </LinearGradientBrush>
    </Button.Background>
    Click Me
</Button>

That works for me, thank you very much.
 
Back
Top Bottom