How to use LinearGradientBrush and Background
Asked Answered
W

3

12

I'm trying to paint a background of my WPF window using LinearGradientBrush, however my code doesn't work. Here is the code

LinearGradientBrush gradientBrush = new  LinearGradientBrush( Color.FromArgb(0, 209, 227, 250),  Color.FromArgb(0, 170, 199, 238), new Point(0.5, 0), new Point(0.5, 1));
Background = gradientBrush;

Unforunatelly my window is still white. Is it possible to change the Background color of the window using code behind ?

Wish answered 11/9, 2011 at 20:40 Comment(2)
Both of your Colors are Transparent so the Background will appear as black unless you have AllowsTransparency set to true. I guess you mean to use 255 instead of 0 for Alpha channel. If you try to set that Background in the Windows constructor and you still can't see it, then it's because some other control in your Window (probably a Panel) has another Background set. Try to set it in a Window without any controls in it.Rolandorolandson
@H.B. No good reason, just started writing it as a comment since the OP said his Window was still White. Probably should have posted an answer instead..Rolandorolandson
G
12

You are setting the alpha setting also. Use this instead since you want the colour:

LinearGradientBrush gradientBrush = new  LinearGradientBrush( Color.FromRgb( 209, 227, 250),  Color.FromRgb(170, 199, 238), new Point(0.5, 0), new Point(0.5, 1));
Background = gradientBrush;
Gerstein answered 24/11, 2012 at 22:8 Comment(0)
A
8
<Border.Background>
  <LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
    <LinearGradientBrush.GradientStops>
      <GradientStop Offset="0.1" Color="{Binding Path=YourBindColor1}" />
      <GradientStop Offset="1" Color="{Binding Path=YourBindColor2}" />
    </LinearGradientBrush.GradientStops>
  </LinearGradientBrush>
</Border.Background>

//Use binding colors
Autotruck answered 18/4, 2018 at 7:35 Comment(0)
G
-1

Setting the Window.Background to a different Brush should work.

Make sure your Background property is not databound to a property via {Binding} directive.

Also, try setting it to a more simpler Brush - for example

Background = new SolidColorBrush(Colors.Black);

Goatsucker answered 11/9, 2011 at 20:48 Comment(1)
But I need to get a gradient effect. If I use SolidColorBrush it will be just one colorWish

© 2022 - 2024 — McMap. All rights reserved.