寻找WPF Grid ColumnSpan行为的解释 [英] Looking for explanation for WPF Grid ColumnSpan behavior

查看:238
本文介绍了寻找WPF Grid ColumnSpan行为的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http:/ /social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c7f5cdf-4351-4969-990f-29ce9ec84b87/ ,但对于一个奇怪的行为仍然缺乏很好的解释。

I asked a question at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c7f5cdf-4351-4969-990f-29ce9ec84b87/ , but still lack a good explanation for a strange behavior.

运行以下XAML将显示列0中的TextBlock的宽度大于100,即使该列设置为宽度100。我认为奇怪可能与它被包装有关在ScrollViewer中,但我不知道为什么。如果我在列上设置了MaxWidth,它可以正常工作,但设置Width不会。

Running the following XAML shows that the TextBlock in column 0 is width greater than 100 even though the column is set to width 100. I think that the strangeness may have something to do with it being wrapped in a ScrollViewer, but I don't know why. If I set a MaxWidth on the columns, it works fine, but setting Width does not.


  1. 为什么列0的宽度不是尊敬的?

  2. 为什么当您移除滚动查看器时,列大小的行为会有所不同?
  1. Why is the width of column 0 not being honored?
  2. Why does the column sizing behave differently when you remove the scroll viewer?

我很感激任何解释!这对我来说是一个真正的难题。

I appreciate any explanation! This is a real puzzle to me.

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300">
    <ScrollViewer HorizontalScrollBarVisibility="Auto" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="100" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <TextBlock x:Name="textBlock" Text="{Binding ElementName=textBlock, Path=ActualWidth}" />
            <TextBlock Text="column 1" Grid.Column="1" />
            <TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="text here that is wider than the first two columns combined" />
        </Grid>
    </ScrollViewer>
</Window>


推荐答案

这是一个非常好的问题,我们的直觉。它揭示了网格布局逻辑的实现细节。

This is a very good question and tests the limits of our intuition. It reveals the implementation details of the Grid's layout logic.

100的宽度没有被遵守,因为:

The width of 100 is not being honored because:


  1. 第三列中没有任何内容会导致网格给出宽度。 第二行中的长文本比第一行中的长文本更宽两列。

  2. 当网格的宽度不受其父节点的限制或设置时,它的布局逻辑显然会延伸第一列而不是最后一列。

通过在第一列上放置MaxWidth,您约束了网格的布局逻辑,因此它移动到第二列并延伸它。你会注意到在这种情况下它会比100宽。

By putting a MaxWidth on the first column, you are constraining the Grid's layout logic, so it moves on to the second column and stretches it. You'll note it will be wider than 100 in that scenario.

但是,只要网格的宽度设置为特定值或受其父(例如当窗口中没有ScrollViewer时),网格的宽度有一个特定的值,第三列的宽度即使设置为空也是如此。现在,网格的自动尺寸代码已停用,并且不再延伸任何列以试图挤入该文本。你可以通过在网格上放置一个特定的宽度来看到它,即使它仍然在ScrollViewer中。

However, as soon as the Grid's width is set to a specific value or is constrained by its parent (e.g. when no ScrollViewer in the Window), the Grid's width has a specific value, and the third column gets a width set even though it is empty. Now the Grid's auto-size code is deactivated, and it no longer stretches any of your columns to try to squeeze in that text. You can see this by putting a specific width on the Grid, even though it is still in the ScrollViewer.

编辑:现在我读了在原始线程中MSDN支持的答案,我相信这是正确的,这意味着这可能是实现附加属性而不是网格本身的结果。然而,原则是一样的,希望我的解释足够清晰,以便理解这里的微妙之处。

Now that I read the answer of the MSDN support in your original thread, I believe it is correct, meaning this is probably the result of the implementation of the attached property and not the grid itself. However, the principle is the same, and hopefully my explanation is clear enough to make sense of the subtlety here.

这篇关于寻找WPF Grid ColumnSpan行为的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆