【问题标题】:Binding the height of a border to its width将边框的高度绑定到其宽度
【发布时间】:2013-01-23 09:54:50
【问题描述】:

我的 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)。

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

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

谢谢,

【问题讨论】:

  • 您在 Visual Studio 的输出窗口中观察到任何绑定错误消息吗?
  • 不,输出中没有关于我的绑定...

标签: wpf windows-phone-7


【解决方案1】:

您尝试绑定到 ActualWidth/ActualHeight 属性。根据一个较早的问题 (here),这是 Silverlight 的一个已知问题,没有简单的解决方法。

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

   <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>

【讨论】:

  • 实际上我只会使用 SizeChanged 事件,因为硬编码宽度并不理想。感谢绑定 ActualWidth 的“错误”之光!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2019-12-25
相关资源
最近更新 更多