I require the size of an element in a Grid layout after changing its ColumnSpan and RowSpan. The ActualWidth and ActualHeight properties do not reflect the rendered size.
<Window x:Class="SizeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" KeyDown="OnKeyDown">
<Grid Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Name="testElement" Content="Test" Background="Red" Grid.Row="0" Grid.Column="0" Grid.RowSpan="1" Grid.ColumnSpan="1" />
</Grid>
private void OnKeyDown(object sender, KeyEventArgs e)
{
var w1 = testElement.ActualWidth;
var h1 = testElement.ActualHeight;
Grid.SetColumnSpan(testElement, 2);
Grid.SetRowSpan(testElement, 2);
var w2 = testElement.ActualWidth;
var h2 = testElement.ActualHeight;
MessageBox.Show(string.Format("Initial size: {0}x{1}\nNew size: {2}x{3}", w1, h1, w2, h2));
}
Output: Initial size: 285.5×160 New size: 285.5×160
ActualWidthandActualHeightwill get you the correct result, when the layout of the grid is updated. So you have to get that two properties in theLayoutUpdatedevent of your grid or other parent elements.