6

我正在尝试创建一个 WPF 应用程序,它由一个 9x9 网格组成,其中行和列使用不同的样式。我希望做的是为列和行定义的高度和宽度提供一个星值。这在当前上下文中似乎不起作用。这甚至可能吗?如果可以,怎么做?

<Window x:Class="BaroqueChessUI.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="500" Width="500">
<Window.Resources>
    <Style x:Key="LightBackground" >
        <Setter Property="Control.Background" Value="Teal" />
    </Style>
    <Style x:Key="DarkBackground" >
        <Setter Property="Control.Background" Value="Maroon" />
    </Style>
    <Style x:Key="FileStyle">
        <Setter Property="Control.Width" Value="0.12" />
    </Style>
    <Style x:Key="RankStyle">
        <Setter Property="Control.Height" Value="0.12" />
    </Style>
    <Style x:Key="FileHeadingStyle">
        <Setter Property="Control.Width" Value="0.04" />
    </Style>
    <Style x:Key="RankHeadingStyle">
        <Setter Property="Control.Height" Value="0.04" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Name="rdRank" Style="{StaticResource FileHeadingStyle}" />
        <RowDefinition Name="rdRank1" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank2" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank3" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank4" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank5" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank6" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank7" Style="{StaticResource FileStyle}" />
        <RowDefinition Name="rdRank8" Style="{StaticResource FileStyle}" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Name="cdFile" Style="{StaticResource RankHeadingStyle}" />
        <ColumnDefinition Name="cdFile2" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile3" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile4" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile5" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile6" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile7" Style="{StaticResource RankStyle}" />
        <ColumnDefinition Name="cdFile8" Style="{StaticResource RankStyle}" />
    </Grid.ColumnDefinitions>
</Grid>

4

2 回答 2

11

网格列定义/行定义定义布局,在定义的区域内,您应该添加应该设置样式的控件(使用附加的属性可能会很乏味),所以尽量不要设置行定义/列定义本身的样式。

造型项目:

您可以像这样在行/列中输入控件(对不起,如果我光顾了):

<Rectangle Grid.Row="0" Grid.Column="0" ></Rectangle>

然后在行/列内的控件上定义样式。

<Rectangle Grid.Row="0" Grid.Column="0" Style="{StaticResource DarkBackground}"></Rectangle>

尺码(星值):

注意:Grid 将根据您的代码动态填充可用区域,如果您为 Grid 定义了固定的高度和宽度并希望按比例分配剩余空间,则只需应用星号值。

换句话说......关于实现“明星价值”:

我希望做的是为列和行定义的高度和宽度提供一个星值。

为什么不直接在定义中输入这样的值?:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Name="rdRank" Height="500" />
            <RowDefinition Name="rdRank1" Height="60*" />
            <RowDefinition Name="rdRank2" Style="40*" />
        </Grid.RowDefinitions>
     </Grid>

在此示例中,名为“rdRank”的行定义将具有固定高度“500”,剩余空间将分配给“rdRank1”,占 60%,“rdRank2”占 40%。

**附加属性:**

以你的风格:

    <Style x:Key="RankStyle">
        <Setter Property="Control.Height" Value="0.12" />
    </Style>

您正在说明应用此样式的项目中具有名为 Height 的属性的任何控件都应采用 0.12 的值。可以这么说,Control.Height 最终会过滤掉。

如果您的目标是在 Row 上实现 0.12* 的高度,请使用:

    <Style x:Key="NewRankStyle" TargetType="{x:Type RowDefinition}">
        <Setter Property="Height" Value="0.12*" />
    </Style>

..

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Name="rdRank"  Style="{StaticResource FileHeadingStyle}" />
        <RowDefinition Name="rdRank1" Style="{StaticResource NewRankStyle}" />

使用“TargetType”允许您定位特定类型的属性,因此允许使用星值。

希望这可以为您澄清一些概念。

于 2009-04-01T03:07:58.727 回答
0

行或列的星号大小仅在您为网格指定宽度和高度时才有效。如果网格根据其内容自动调整大小,则星号大小不起作用。

于 2009-08-21T22:52:06.690 回答