WPF 可変サイズWindowのサイズ変更制限

ユーザの操作によりWindowサイズ変更制限するために、以下のXAMLコードを試してみた。

<Window x:Class="Test.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Test"
  Title="MainWindow" Height="300" Width="400"
  MaxWidth="{Binding RelativeSource={RelativeSource Self}, Path=Width}"
  MinWidth="{Binding RelativeSource={RelativeSource Self}, Path=Width}">
</Window>

6, 7行目がポイント。横幅の最小値、最大値を現在の横幅にバインドすることで、コードから動的に横幅を変更した場合でも自動で値が設定し直される。