【问题标题】:Keep grid star size as a constant in XAML在 XAML 中将网格星大小保持为常数
【发布时间】:2014-02-16 21:13:44
【问题描述】:
<Grid.RowDefinitions>
    <RowDefinition Height="4*"/>
    <RowDefinition Height="3*"/>
</Grid.RowDefinitions>

我想在 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>

当然,这段代码是不正确的,不会产生预期的效果。我怎样才能正确地做到这一点?

【问题讨论】:

    标签: wpf visual-studio-2010 xaml resources


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2021-05-04
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多