Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 为什么WrapPanel会破坏网格';哥伦布的定义?_Wpf - Fatal编程技术网

Wpf 为什么WrapPanel会破坏网格';哥伦布的定义?

Wpf 为什么WrapPanel会破坏网格';哥伦布的定义?,wpf,Wpf,这是我经常使用的一种设计,一个带有相对大小列的网格(使用星号表示宽度),以模拟相对边距或其他类似任务 一个类似于我现在正在做的事情的例子: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

这是我经常使用的一种设计,一个带有相对大小列的网格(使用星号表示宽度),以模拟相对边距或其他类似任务

一个类似于我现在正在做的事情的例子:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="1234567890123456789012345678901234567890123456789012345678901234567890" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap"/>

        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Text="AAA" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" FontWeight="DemiBold"/>
    </Grid>
</Grid>
</Window>

这是它的输出

这正是我所期望的

但是,如果我只是用一个WrapPanel更改根网格,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<WrapPanel>
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="1234567890123456789012345678901234567890123456789012345678901234567890" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap"/>

        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Text="AAA" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" FontWeight="DemiBold"/>
    </Grid>
</WrapPanel>
</Window>

然后我得到这个:


好的,由于WrapPanel布局系统的原因,在位置上显然存在一些差异,这对我来说是可以的,甚至是可以预期的,但是……列到底发生了什么事?

除非
网格实际具有
宽度,否则无法计算列的相对大小。我不确定您为什么要在此处使用
WrapPanel
,但您可以指定
网格的固定
宽度
最小宽度

<WrapPanel>
    <Grid x:Name="grid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="True"
          Width="300">

是的,好的,但这是一个站不住脚的解释:网格在某一点上有一个宽度。Measure/Arrange系统甚至可以执行10次传递,但仍会设置并更新grid ActualWidth属性,并在调整大小时重新计算列宽。简单地说,当承载在包裹面板内以及承载在网格、堆栈面板、ContentPresenter、边框或停靠面板内时,is没有由我设置的宽度。在相同的条件下,当主体位于包裹面板中时,它在所有情况下均按预期工作。为什么?