1

我的 Windows Phone 应用程序的布局有一个奇怪的问题。我有一个网格,有 4 列。在每个里面,我放了一个边框,我希望这些边框是完美的正方形(即使我的容器高度发生变化......)。

以下是相关代码:

<Grid Grid.Row="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="Red"  Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource self}}"><TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" /></Border>
<Border Grid.Column="1" Background="Red" Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource self}}"><TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" /></Border>
<Border Grid.Column="2" Background="Red" Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource self}}"><TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" /></Border>
<Border Grid.Column="3" Background="Red" Height="{Binding Path=ActualWidth, RelativeSource={RelativeSource self}}"><TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" /></Border>

</Grid>

它在 Blend 中表现出色:

在此处输入图像描述

但是当我在模拟器上运行它时,我的边界消失了(看起来高度变成了 0)。

我没有任何代码隐藏代码..

有人对我的问题有任何想法吗?

谢谢,

4

1 回答 1

1

您尝试绑定到 ActualWidth/ActualHeight 属性。从一个较旧的问题(此处)来看,这是 Silverlight 的一个已知问题,并且没有简单的解决方法。

尝试其他方法,例如绑定到 Width 并指定列宽:

   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Grid.Column="0" Background="Red"  Width="100" Height="{Binding Path=Width, RelativeSource={RelativeSource self}}">
            <TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>
        <Border Grid.Column="1" Background="Red"  Width="100" Height="{Binding Path=Width, RelativeSource={RelativeSource self}}">
            <TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>
        <Border Grid.Column="2" Background="Red"  Width="100" Height="{Binding Path=Width, RelativeSource={RelativeSource self}}">
            <TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>
        <Border Grid.Column="3" Background="Red"  Width="100" Height="{Binding Path=Width, RelativeSource={RelativeSource self}}">
            <TextBlock Text="TextBlock" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Border>

    </Grid>
于 2013-01-23T10:31:24.833 回答