Grid
更改布局后,我需要布局中元素的大小ColumnSpan
and RowSpan
。和属性ActualWidth
不ActualHeight
反映渲染大小。
<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));
}
输出:初始尺寸:285.5x160 新尺寸:285.5x160