2014-02-16 47 views
1
<Grid.RowDefinitions> 
    <RowDefinition Height="4*"/> 
    <RowDefinition Height="3*"/> 
</Grid.RowDefinitions> 

我想在XAML中的其他位置定義4/3比例,然後使用它。像這樣:在XAML中將網格星形大小保持爲常量

<System:Double x:Key="Top_Part">4</System:Double> 
<System:Double x:Key="Bottom_Part">3</System:Double> 

<Grid.RowDefinitions> 
    <RowDefinition Height="{StaticResource Top_Part}"/> 
    <RowDefinition Height="{StaticResource Bottom_Part}"/> 
</Grid.RowDefinitions> 

當然,這段代碼是不正確的,不會產生預期的效果。我怎樣才能正確地做到這一點?

回答

5

的類型RowDefinitionHeight property的是GridLength所以你需要在你的資源,創造GridLength實例:

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Window.Resources> 
    <GridLength x:Key="Top_Part">4*</GridLength> 
    <GridLength x:Key="Bottom_Part">3*</GridLength > 
    </Window.Resources> 
    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="{StaticResource Top_Part}"/> 
     <RowDefinition Height="{StaticResource Bottom_Part}"/> 
    </Grid.RowDefinitions> 
    <Grid Background="Blue" Grid.Row="0"/> 
    <Grid Background="Red" Grid.Row="1"/> 
    </Grid> 
</Window>