Neil Derno
Neil Derno

Reputation: 87

WPF C#: Setting background color of a WPF app not setting fully

I made a WPF app (WindowStyle="None" hence no default windows buttons like exit, maximize, minimize)

It doesn't set the color (in my case, it's black) to the entire frame: Error

As you can see, there is a bit of white gap. (It's not a margin, I tested for this).

<Window x:Class="FancyGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowStyle="None"
        Title="MainWindow" Height="250" Width="340"
        Background="Black">

</Window>

Upvotes: 1

Views: 1629

Answers (2)

JDT
JDT

Reputation: 31

I'm noticing that the typical chrome is removed around the window.

I recreated the window with the code you provided and I see this: black background with window chrome

Is there a resource file being referenced that may be affecting the window?

Also, you have Live Visual Tree running. Try clicking the middle button and then try clicking on the white bar. If you can select it, you should see it selected in the VS Live Visual Tree window.

activate live visual tree

Edit... After seeing Jcl's post about the ResizeMode="NoResize" I gave it a try. Sure enough, that's the secret.

enter image description here

Thanks Jcl!

Upvotes: 3

Jcl
Jcl

Reputation: 28272

You need ResizeMode="NoResize" in your window, otherwise there's a title bar shown (empty in your case), even if you set WindowStyle="None".

  • With WindowStyle="None" only:

enter image description here

(notice the blue "gap" as you call it on top... that's your white gap, just that my theme is blue)

  • With ResizeMode="NoResize":

enter image description here

(no gap, but can't resize the window now)

  • Or if you still want it to be resized (have the resize grip on it), but not show that title bar, set AllowsTransparency="True" and ResizeMode="CanResizeWithGrip":

enter image description here

(notice the grip for resizing on bottom right)

Note that having AllowsTransparency=true may have side effects. If any of those side effects is a problem to you, you can implement the resizing yourself by creating your own (black in your case) border and send the drag/resize messages. Expand on whether you need this or not and I'll show you how.

Upvotes: 3

Related Questions