Alex F
Alex F

Reputation: 43311

Keep grid star size as a constant in XAML

<Grid.RowDefinitions>
    <RowDefinition Height="4*"/>
    <RowDefinition Height="3*"/>
</Grid.RowDefinitions>

I want to define 4/3 ratio somewhere else in XAML, and then use it. Something like this:

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

Of course, this code is incorrect and doesn't produce desired effect. How can I do this correctly?

Upvotes: 1

Views: 611

Answers (1)

nemesv
nemesv

Reputation: 139768

The type of the Height property of the RowDefinition is GridLength so you need to create GridLength instances in your resources:

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

Upvotes: 5

Related Questions