11

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 ?

wpflerner
  • 111
  • 1
  • 1
  • 3
  • 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. – Fredrik Hedblad Sep 11 '11 at 21:09
  • @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.. – Fredrik Hedblad Sep 11 '11 at 21:46

3 Answers3

11

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;
Oltamor
  • 121
  • 1
  • 4
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
Sabuncu
  • 5,095
  • 5
  • 55
  • 89
xrhstos
  • 101
  • 1
  • 3
0

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);

Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174