AutoWindowPlacement.WPF 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoWindowPlacement.WPF --version 1.0.0
                    
NuGet\Install-Package AutoWindowPlacement.WPF -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoWindowPlacement.WPF" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoWindowPlacement.WPF" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AutoWindowPlacement.WPF" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AutoWindowPlacement.WPF --version 1.0.0
                    
#r "nuget: AutoWindowPlacement.WPF, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AutoWindowPlacement.WPF@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AutoWindowPlacement.WPF&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AutoWindowPlacement.WPF&version=1.0.0
                    
Install as a Cake Tool

AutoWindowPlacement.WPF

A lightweight library that automatically saves and restores WPF window positions and states between application sessions.

NuGet License: MIT

Features

  • Automatic window placement persistence - Save and restore window position, size, and state
  • Registry-based storage - Built-in storage using Windows Registry
  • Extensible storage - Implement custom storage strategies (JSON, XML, database, etc.)
  • Simple XAML integration - Add with a single line of code
  • Zero dependencies - Pure WPF implementation

Supported Frameworks

  • .NET 5.0-windows, 6.0-windows, 7.0-windows, 8.0-windows

Installation

Install via NuGet Package Manager:

Install-Package AutoWindowPlacement.WPF

Or via .NET CLI:

dotnet add package AutoWindowPlacement.WPF

Quick Start

Add the namespace to your Window XAML:

<Window x:Class="YourApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:awp="clr-namespace:NullSoftware.Windows.Extensions;assembly=awp_wpf"
        awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage}"
        Title="Main Window" Height="450" Width="800">
    
</Window>

That's it! The window position and state will now be automatically saved to the Windows Registry and restored on next launch.

Usage

Basic Usage with Registry Storage

The simplest way to use AutoWindowPlacement is with the built-in RegistryStorage:

<Window xmlns:awp="clr-namespace:NullSoftware.Windows.Extensions;assembly=awp_wpf"
        awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage}">
</Window>

By default, this stores window placement in:

HKEY_CURRENT_USER\SOFTWARE\{CompanyName}\{ProductName}\{WindowName}.Placement

Custom Registry Location

Customize the registry key and naming format:

<Window awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage 
            Key='SOFTWARE\\MyCompany\\MyApp\\WindowSettings',
            NameFormat='{0}_Position'}">
</Window>

Custom Storage Implementation

Implement the IWindowPlacementStorage interface for custom storage:

using System.Windows;
using NullSoftware.Windows;

public class JsonFileStorage : IWindowPlacementStorage
{
    private readonly string _filePath;

    public JsonFileStorage(string filePath)
    {
        _filePath = filePath;
    }

    public byte[]? LoadPlacement(Window window)
    {
        // Load from JSON file
        // Return byte array or null if not found
    }

    public void SavePlacement(Window window, byte[] serializedPlacement)
    {
        // Save to JSON file
    }
}

Then use it in code-behind:

public MainWindow()
{
    InitializeComponent();
    WindowExtensions.SetPlacementStorageStrategy(this, new JsonFileStorage("settings.json"));
}

API Reference

WindowExtensions

The main class providing attached properties for window placement.

Attached Properties
  • PlacementStorageStrategy - Gets or sets the storage strategy for window placement
WindowExtensions.SetPlacementStorageStrategy(window, storageInstance);
IWindowPlacementStorage storage = WindowExtensions.GetPlacementStorageStrategy(window);

IWindowPlacementStorage

Interface for implementing custom storage strategies.

public interface IWindowPlacementStorage
{
    void SavePlacement(Window window, byte[] serializedPlacement);
    byte[]? LoadPlacement(Window window);
}

RegistryStorage

Built-in implementation that stores window placement in Windows Registry.

Properties
  • Hive - Registry hive (default: RegistryHive.CurrentUser)
  • Key - Registry key path (default: SOFTWARE\{Company}\{Product})
  • NameFormat - Value name format (default: {0}.Placement)
Methods
  • GetSettingKey(Window) - Override to customize the registry value name
  • ProvideDefaultHive() - Override to change default registry hive
  • ProvideDefaultKey() - Override to change default registry key path

WindowPlacementManager

Low-level API for direct window placement manipulation.

// Get window placement
var placement = WindowPlacementManager.GetPlacement(window);

// Set window placement
WindowPlacementManager.SetPlacement(window, placement);

// Serialize placement to bytes
byte[] data = WindowPlacementManager.Serialize(placement);

// Deserialize placement from bytes
var placement = WindowPlacementManager.Deserialize(data);

Advanced Examples

Per-User Storage with Custom Key

<Window awp:WindowExtensions.PlacementStorageStrategy="{awp:RegistryStorage 
            Key='SOFTWARE\\MyApp\\Settings',
            Hive='CurrentUser'}">
</Window>

Conditional Storage in Code-Behind

public MainWindow()
{
    InitializeComponent();
    
    if (Settings.Default.RememberWindowPosition)
    {
        WindowExtensions.SetPlacementStorageStrategy(this, new RegistryStorage());
    }
}

Custom Storage with Inheritance

public class CustomRegistryStorage : RegistryStorage
{
    protected override string GetSettingKey(Window window)
    {
        // Use window title instead of type name
        return string.Format(NameFormat, window.Title.Replace(" ", "_"));
    }

    protected override string ProvideDefaultKey()
    {
        return @"SOFTWARE\MyCompany\MyApp\Windows";
    }
}

How It Works

  1. When PlacementStorageStrategy is set, the library hooks into window events
  2. On SourceInitialized, it loads the saved placement and applies it to the window
  3. On Closing, it captures the current placement and saves it via the storage strategy
  4. Window placement includes: position, size, and state (normal/maximized/minimized)

Design-Time Support

The library automatically detects design-time mode and disables itself in the Visual Studio designer, ensuring a smooth design experience.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Developed by Null Software

Repository

https://github.com/nullsoftware/AutoWindowPlacement.WPF

Product Compatible and additional computed target framework versions.
.NET net5.0-windows7.0 is compatible.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0-windows7.0

    • No dependencies.
  • net6.0-windows7.0

    • No dependencies.
  • net7.0-windows7.0

    • No dependencies.
  • net8.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.1 29 2/14/2026
1.0.0 27 2/14/2026