2013-01-23 54 views
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中呈現很大:

enter image description here

但是,當我在模擬器上運行它,我的邊框是走了(貌似高度達到0)。

我沒有對任何代碼隱藏代碼..

不要任何人對我的問題有什麼想法?

Thanxs,

+0

你觀察Visual Studio的輸出窗口任何約束力的錯誤消息? – Clemens

+0

不,沒有在輸出關於我的綁定... – jmindark

回答

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

其實我只會使用SizeChanged事件,因爲硬編碼寬度並不理想。 Thanxs爲具有綁定ActualWidth的「bug」指示燈! – jmindark

相關問題