xamlnewb
xamlnewb

Reputation:

How do I set the size of a WPF window based on a desired client area size?

I know that in XAML/WPF, I can set the Window size using the Width and Height properties, but suppose I want to set the window size such that the client area (minus the Windows borders/decorations) will be a certain fixed size, what is the easiest way to do that?

Like, suppose I want the client area of the window to be 640x480, ala this:

    <Window x:Class="SomeProject.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Background="Black">

<Canvas Width="640" Height="480"/>
</Window>

How can I ensure the inital Window size is such that it has a 640x480 client size that fits the child canvas perfectly? I'm only really interested in how to easily set this up for startup time, not so worried about how to deal with resize events and such.

Thanks.

Upvotes: 2

Views: 9784

Answers (1)

Rich
Rich

Reputation: 36856

Add the following attribute to the window

SizeToContent="WidthAndHeight"

Upvotes: 8

Related Questions