【问题标题】:Render size of an element after changing RowSpan and ColumnSpan更改 RowSpan 和 ColumnSpan 后呈现元素的大小
【发布时间】:2013-07-05 06:52:05
【问题描述】:

在更改ColumnSpanRowSpan 后,我需要Grid 布局中元素的大小。 ActualWidthActualHeight 属性不反映呈现的大小。

<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

【问题讨论】:

  • 你在设置新的 Span 后尝试过grid.UpdateLayout() 吗?

标签: c# wpf grid size html-table


【解决方案1】:

ActualWidthActualHeight 将在更新网格布局时为您提供正确的结果。因此,您必须在网格或其他父元素的 LayoutUpdated 事件中获取这两个属性。

【讨论】:

    【解决方案2】:

    调用Grid.UpdateLayout Method ActualWidthActualHeight 后将反映新值。

    Grid.SetColumnSpan(testElement, 2);
    Grid.SetRowSpan(testElement, 2);
    grid.UpdateLayout()
    

    【讨论】:

      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多