I'm trying to calculate StackPanel
width
, height
(located in middle cell of the grid) before window showing (for example in window constructor). How it can be achieved?
<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>
Measure for Window also as for stackPanel sets DesiredSize
to {0;0}
public partial class TestWindow : Window
{
public TestWindow()
{
InitializeComponent();
this.Measure(new Size(this.Width, this.Height)); // -> this.DesiredSize = {0;0}
...
}
}
EDIT1
The following works for FixedPage:
fixedPage.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
fixedPage.Arrange(new Rect(0, 0, fixedPage.DesiredSize.Width, fixedPage.DesiredSize.Height));
Then we can access stackPanel.ActualWidth
and stackpanel.ActualHeight
.
But for the Window, it does not works.