#1,069 – Main Window Initially Has Keyboard Focus

When you first start a WPF application which contains elements that can get keyboard focus (e.g. TextBox), it’s the main Window that has the keyboard focus when the application starts.

We can see this by using code that reports the current keyboard focus within a Label at the bottom of the window.


If we attach an event handler to the main Window for the TextInput event and use the handler to log information about the event, we can start typing after the window comes up and see that the Window is getting TextInput events based on what we type.  Nothing is rendered to the screen, but our logging indicates that TextInput events are being fired, with the main window as their source.

        private void Window_TextInput(object sender, TextCompositionEventArgs e)
            Trace.WriteLine(string.Format("Window_TextInput: [{0}], source={1}", e.Text, e.Source.ToString()));


#837 – A Window Cannot Be a Child of a ContentControl

The content of a ContentControl can be just about anything, including either UI elements or simple CLR objects.  For example, the content of a CheckBox can be something more than a simple text string.

One of the few elements that cannot be used as content for a ContentControl is a Window.  It wouldn’t make sense to place a Window element as the content of something else, like a Button.  A Window can only be the topmost element of the logical tree.

If you do try to include a Window as a child of another element, you’ll get an exception at run-time, when the XAML is parsed.

    <Button Height="23" Width="80">
        <Window Title="A window in a button?" />


#283 – A Window Can Have Only a Single Child Element

In WPF, a Window can have only a single child element.  Window inherits from ContentControl, which is a control that contains a single child element, referenced by its Content property.

Your window might have a single simple child control, like a Button.

	<Button Content="Big Daddy Button"/>

Since having a single control in your application is not very useful, it’s more common that the single child control of a Window is a container control like StackPanel, which in turn can contain multiple child elements.

		<Label Content="You can enter your name here:"/>
		<TextBox />
		<Button Content="Push to Continue"/>

#272 – Displaying a Border Around a Window

You can include a border around the edges of a Window using the BorderBrush and BorderThickness properties.

By default, the BorderBrush is null and BorderThickness is a Thickness structure with all of its dimensions set to 0.

If you specify only the BorderThickness property, you get a black border around the window.


You can also specify both a border brush and a border thickness.


            <GradientStop Color="DarkKhaki" Offset="0.0"/>
            <GradientStop Color="DarkGreen" Offset="1.0"/>

#271 – Changing a Window’s Resize Behavior

There are four different options for allowing a window to be resized.  You set the resizing behavior using the ResizeMode property.

The default value for ResizeMode is CanResize.  This results in the standard window border, allowing resizing by dragging the edge of the window.

	Width="300" Height="150" ResizeMode="CanResize">

The CanResizeWithGrip option is similar to CanResize, but the lower right corner of the window shows a little “grip” icon indicating that you can “grab” the window here to resize it.

	Width="300" Height="150" ResizeMode="CanResizeWithGrip">

The NoResize option creates a window that can’t be resized, minimized or maximized.  Notice that the minimize and maximize buttons have also disappeared.

	Width="300" Height="150" ResizeMode="NoResize">

The final choice for ResizeMode is CanMinimize, which creates a window that can’t be resized or maximized, but can be minimized.

	Width="300" Height="150" ResizeMode="CanMinimize">

#270 – Minimum and Maximum Window Size

By default, there are no upper or lower limits for the size of a window in WPF–you can shrink a window to the point where none of its content is visible and you can maximize a window so that it takes up the entire desktop.

You can use the MinWidth and MinHeight properties to dictate the minimum dimensions for the window.  When resizing the window, you’ll be unable to make the window any smaller.

	Width="400" Height="300" MinHeight="100" MinWidth="200">

You can use the MaxWidth and MaxHeight properties to dictate the maximum dimensions for the window.  When resizing the window, you’ll be unable to make the window any larger.  If you try maximizing the window, it will be constrained to the maximum size.

	Width="400" Height="300" MaxWidth="500" MaxHeight="320">

#269 – Automatically Sizing a Window to Fit Its Contents

You normally set the width and height of a WPF Window by setting its Width and Height properties directly.

<Window Width="400" Height="300" >

At runtime, this sets the starting window size.

If you want the window to automatically size to fit its contents, you can set the SizeToContent property.  By default, this property has a value of Manual, indicating that the Width and Height properties will dictate the window’s size.  (Ignoring the effect of min/max properties).

Setting SizeToContent to Height will force the window to adjust its height to fit the contents.

<Window Width="400" Height="300" SizeToContent="Height">

Setting SizeToContent to Width will force the window to adjust its width to fit the contents.

<Window Width="400" Height="300" SizeToContent="Width">

And setting it to WidthAndHeight will tell the window to adjust both dimensions.

<Window Width="400" Height="300" SizeToContent="Width">