petko_stankoski
petko_stankoski

Reputation: 10723

Background turns grey when opaque

Here's my code:

var windows = Application.Current.Windows;
var backgroundWindow = windows[windows.Count - 1];
backgroundWindow.Background = Brushes.White;
backgroundWindow.Opacity = 0.5;

SpecialLettersLayout layout = new SpecialLettersLayout((SmartButton)button, KeyboardViewModel);
layout.Show();

When I open a new window, I want the old one (the one in the background to have an opacity of 0.5. When I run the app, here's what I get:

enter image description here

As you can see, the background is kinda grey. How can I replace the grey color with white?

Upvotes: 0

Views: 804

Answers (2)

petko_stankoski
petko_stankoski

Reputation: 10723

What fixed my problem was adding this:

AllowsTransparency="True"

in my window. @Sheridan was right though, when I set:

backgroundWindow.Background = Brushes.Transparent;

I got black background. I don't know why, I never set the background of any of my windows to black.

Upvotes: 1

Sheridan
Sheridan

Reputation: 69985

Your Window.Background is turning grey when you set the Opacity to 0.5 because the actual background of the Window is Black. You can verify this by setting the following:

backgroundWindow.Background = Brushes.Transparent;

Instead of setting the Window.Opacity to 0.5, set the Opacity of any container control to 0.5 so that you are not making the White Window.Background opaque, but making the container opaque instead, leaving the White Window.Background:

<Window ...>
    <Grid>
        <Rectangle Fill="White" Opacity="0.5" />
        <!-- Your main content -->
    </Grid>
</Window>

Upvotes: 1

Related Questions