3

I'm trying to play around with colors in WPF and the form's background color. Without doing ANYTHING ELSE (no code behind, deriving from another class, etc), I create a brand new default Windows Form. I change the background to some light blue color. Save the form. Run the program and open that form.. The form shows up in some other much darker color like it's not even respecting the XAML of

<Window x:Class="MyProjectNamespace.AnotherWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AnotherWindow" Height="300" Width="300" Background="#4800A8A6">
    <Grid>

    </Grid>
</Window>

Additionally, if I try to take this same background value of #4800A8A6 and try to create a brush and do a BrushConverter.ConvertFrom( "#4800A8A6" ); and run the form, I STILL get the incorrect color as displayed via the designer... what gives...

H.B.
  • 166,899
  • 29
  • 327
  • 400
DRapp
  • 47,638
  • 12
  • 72
  • 142

3 Answers3

5

The first byte of your color code, the '48' is an alpha value, so you're allowing alpha blending on your Window (not a "Windows Form" btw, that's a different technology ;) ), which means the color of your window is going to be blended with the colors of things behind it.

Try changing to: #FF00A8A6, see if that gives you a better result.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • Your answer actually was the more complete by the explicit inclusion of alpha "FF" value prefixing to prevent any "bleeding" and accounting for the 4th possible hex color value. I'll check as soon as 5 minutes are up from posting time. – DRapp Sep 22 '11 at 18:36
  • @DRapp: avoid specifying the alpha channel entirely and use the [`Brush.Opacity`](http://msdn.microsoft.com/en-us/library/system.windows.media.brush.opacity.aspx) dependency property instead. This way it is more explicit when you need transparency (avoiding problems like your own in the future). – user7116 Sep 22 '11 at 18:58
3

Your problem is the inclusion of an Alpha channel in the background color (i.e. #AARRGGBB). In the designer you're seeing the background being composited against whatever the background color is in VS (in my case White), which will "lighten" the background in comparison to when you run it as standalone.

To see this for yourself, try Background="#00A8A6", or you could try adding AllowsTransparency="True" (but this has an onerous requirement of WindowStyle.None).

user7116
  • 63,008
  • 17
  • 141
  • 172
  • I'll take your suggestion about Opacity into consideration. Thanks for clarifying how your solution would also work for me. – DRapp Sep 22 '11 at 19:44
2

That's probably because of the alpha component in your color. If you are trying to make your windows transparent, you have to use AllowsTransparency="True". If you don't, WPF will merge the default windows background color with your windows background.

alf
  • 18,372
  • 10
  • 61
  • 92