ウィンドウが表示される前に(たとえば、ウィンドウコンストラクターで)、(グリッドの中央のセルにある)StackPanel
width
を計算しようとしています。height
どのようにそれを達成することができますか?
<Window x:Class="WpfApplication2.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TestWindow" Height="300" Width="300">
<Grid Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Grid.Column="1" Name="stackPanel"></StackPanel>
</Grid>
stackPanelセットDesiredSize
と同様にWindowを測定します{0;0}
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
this.Measure(new Size(this.Width, this.Height)); // -> this.DesiredSize = {0;0}
...
}
}
編集1
FixedPageでは次のように機能します。
fixedPage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
fixedPage.Arrange(new Rect(0, 0, fixedPage.DesiredSize.Width, fixedPage.DesiredSize.Height));
次に、にアクセスできstackPanel.ActualWidth
ますstackpanel.ActualHeight
。
ただし、ウィンドウの場合は機能しません。