How to use LinearGradientBrush and Background

wpflerner picture wpflerner · Sep 11, 2011 · Viewed 21.4k times · Source

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 ?

Answer

Oltamor picture Oltamor · Nov 24, 2012

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;