按钮没有填充网格单元格

时间:2012-07-18 21:42:42

标签: silverlight windows-phone-7

我正在尝试在Silverlight中构建一个简单的3x3网格,每个网格单元格中都有一个按钮。网格定义如下。当我向网格添加按钮时,它们永远不会填充130x130网格单元格。我在按钮上设置边距和填充为0,并将它们的水平和垂直对齐设置为拉伸。

    <Grid x:Name="Test" ShowGridLines="True" HorizontalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="130"></ColumnDefinition>
            <ColumnDefinition Width="130"></ColumnDefinition>
            <ColumnDefinition Width="130"></ColumnDefinition>
        </Grid.ColumnDefinitions>

    <Style x:Key="OperandButton" TargetType="Button">
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Background" Value="{StaticResource PhoneAccentColor}" />
        <Setter Property="FontSize" Value="50" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
    </Style>

 <Button Content="10" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="0" />
 <Button Content="3" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="1" />
 <Button Content="7" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="2" />

4 个答案:

答案 0 :(得分:1)

您的代码应该可以正常工作。只需按原样尝试此代码。

 <Grid x:Name="Test" ShowGridLines="True" HorizontalAlignment="Center" >
            <Grid.Resources>
                <Style x:Key="OperandButton" TargetType="Button">
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Background" Value="Blue" />
                    <Setter Property="FontSize" Value="50" />
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="VerticalAlignment" Value="Stretch" />
                </Style>
            </Grid.Resources>
                <Grid.RowDefinitions>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
            <RowDefinition Height="130"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"></ColumnDefinition>
            <ColumnDefinition Width="130"></ColumnDefinition>
            <ColumnDefinition Width="130"></ColumnDefinition>
        </Grid.ColumnDefinitions>   
        <Button Content="10" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="0" />
        <Button Content="3" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="1" />
        <Button Content="7" Style="{StaticResource OperandButton}" Grid.Row="0" Grid.Column="2" />    
        </Grid>

如果你还没有得到它,请告诉我。

答案 1 :(得分:0)

尝试以下方法,

在Blend中打开项目时。右键单击按钮,转到编辑模板,编辑当前。你会发现每个按钮都有一个网格和一个容器。使两者的大小相等,你会看到默认边框的大小与按钮大小相同。

同样的模板也可以用于其他按钮。

现在尝试包围网格中的按钮。

答案 2 :(得分:0)

查看混合中的模板解决了问题。在边框上设置了以下属性:

Margin="{StaticResource PhoneTouchTargetOverhang}"

一旦设置为0,网格就完美了。感谢您的指导。

答案 3 :(得分:0)

尝试此操作时,不得指定VerticalAlignmentHorizontalAlignment

Margin="-10"
相关问题