次の 2 つの方法のいずれかで、Load イベント ハンドラーのコード ビハインドで実行できます。
注: LayoutRoot Grid の内容は両方の例で同じですが、LayoutRoot の幅と高さは例 A でのみ指定されています。
A) Window の SizeToContent およびコンテンツの Width と Height の ClearValue:
using System.Windows;
namespace WpfWindowBorderTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ClearValue(SizeToContentProperty);
LayoutRoot.ClearValue(WidthProperty);
LayoutRoot.ClearValue(HeightProperty);
}
}
}
次のようなページ レイアウトを想定します (Window の SizeToContent 設定と Loaded イベント ハンドラー、および LayoutRoot で指定された Width と Height に注意してください)。
<Window x:Class="WpfWindowBorderTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">
<Grid x:Name="LayoutRoot" Width="300" Height="300" Background="Green">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
<Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
<Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
<Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
</Grid>
</Window>
また
B) システム固有のクライアント ウィンドウ フレーム サイズを考慮して、ウィンドウの幅と高さを設定します。
System.Windows を使用します。
namespace WpfWindowBorderTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
const int snugContentWidth = 300;
const int snugContentHeight = 300;
var horizontalBorderHeight = SystemParameters.ResizeFrameHorizontalBorderHeight;
var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
var captionHeight = SystemParameters.CaptionHeight;
Width = snugContentWidth + 2 * verticalBorderWidth;
Height = snugContentHeight + captionHeight + 2 * horizontalBorderHeight;
}
}
}
次のようなプロポーショナル ページ レイアウトを想定しています (Window の SizeToContent 設定または Loaded イベント ハンドラー、または LayoutRoot で指定された Width と Height に注意してください)。
<Window x:Class="WpfWindowBorderTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1">
<Grid x:Name="LayoutRoot" Background="Green">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
<Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
<Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
<Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
</Grid>
</Window>
XAML で宣言的に行う方法はまだ思いつきません。