WPF Window Size, Position, and Style Persistence

Download the WindowPersistance Source Code

The following article shows a simple way to create window and control persistence in WPF.

For this example user should have already made a WPF window application. Next step is to add settings to the application properties.


Note that WindowSplitter property is optional as it sets the grid splitter position and is application specific for the current example.

Next step is to add the bindings to the Window properties in MainWindow.xaml.

Before we can bind to the application properties we have to expose the properties class by declaring:

This will allow us to bind directly to the declared properties in the application settings:
Height="{Binding WindowHeight, Source={x:Static Properties:Settings.Default}, Mode=TwoWay}"

The advantage of binding Window size, position, and state directly to the application properties is that any time size, position, or state are changed the properties are automatically updated leaving us with less C# code.

The binding can also be done on internal contents of a window – in this case the GridSplitter control.

Notice that the heigh of the last grid row is bound to the WindowSplitterPosition settings. This binding will make the GridSplitter position persistent.

Only C# code needed is for saving of the application settings when the MainWindow is closed. This is done by adding a handler for the Window.Closed event.

If an application has multiple windows that need persistence then a solution may involve using user created Application settings as described in the following MSDN article.