在XAML中将网格星大小保持为常量

Ale*_*x F 1 wpf resources xaml visual-studio-2010

<Grid.RowDefinitions>
    <RowDefinition Height="4*"/>
    <RowDefinition Height="3*"/>
</Grid.RowDefinitions>
Run Code Online (Sandbox Code Playgroud)

我想在XAML中的其他地方定义4/3比率,然后使用它.像这样的东西:

<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>
Run Code Online (Sandbox Code Playgroud)

当然,这段代码不正确,不会产生预期的效果.我该怎么做才能正确?

nem*_*esv 5

该类型的Height属性RowDefinitionGridLength,所以你需要建立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>
Run Code Online (Sandbox Code Playgroud)