2012-10-03 29 views
1

我想將圖像的Width綁定到它的ActualHeight(高度取決於佈局,我希望它是方形的 - 如果有更好的方法做到這一點,我很樂意聽)。綁定寬度到ActualHeight - 只能部分工作

這是我的XAML:

<Grid Grid.Row="1" VerticalAlignment="Stretch"> 
<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="auto"/> 
    <ColumnDefinition Width="auto"/> 
</Grid.ColumnDefinitions> 
<Grid.RowDefinitions> 
    <RowDefinition Height="*"/> 
    <RowDefinition Height="*"/> 
</Grid.RowDefinitions> 
<Image x:Name="favImage1" Source="{Binding Controller.FailStorage.FailRoamingStorage.Favorites[0].ImageUri}" Grid.Column="0" Grid.Row="0" Style="{StaticResource FavoriteImageStyle}" Width="{Binding ActualHeight, ElementName=favImage1, Mode=OneWay}"/> 
</Grid> 

風格被定義爲這樣的:

<Style x:Key="FavoriteImageStyle" TargetType="Image"> 
     <Setter Property="Stretch" Value="UniformToFill"/> 
     <Setter Property="Margin" Value="0,0,12,12"/> 
     <Setter Property="VerticalAlignment" Value="Stretch"></Setter> 
    </Style> 

現在,這個工程部分 - 將項目添加到集合後導航回到頁面似乎工作。

但是,任何加載或隨後導航到頁面都會使圖像無法顯示。

有趣的是,加載後,圖像的ActualWidth不是ActualHeight。它更少。和電網的寬度還要少...這只是普通的怪異......

favImage1.ActualWidth

185.13514709472656

favImage1.ActualHeight

274.0

(( FrameworkElement)favImage1.Parent).ActualWidth

138.0

這樣做的正確方法是什麼?

+0

你可以設置寬度值在某些觸發? –

+0

將ActualHeight綁定到觸發器上的屬性是什麼意思? –

回答

0

如果我理解你正確,你想確保你的圖像保持正方形。

我認爲這是不正確的方式來操縱xaml中的圖像。我寧願換圖像中的Border並設置Border的大小的東西固定,然後將圖像Stretch屬性更改爲UniformToFill

<Border Width="110" Height="110"> 
    <Image Source="my.jpg" Stretch="UniformToFill" /> 
</Border> 

另一種選擇是使用ViewBox包裹的圖像。在這裏,你可以看到不同值的影響爲Stretch屬性:

ViewBox.Stretch Property

+0

感謝您的回覆。這並沒有幫助我,因爲我需要通過佈局來設置高度(VerticalAlignment = Stretch on其中一個更高的元素(我將編輯爲更具體)。因此,我實際上不能指定任何維度。注意到我在帖子的頂部說過:「高度取決於佈局,我希望它是方形的」) –