Silverlight 按钮未填充网格单元

Silverlight 按钮未填充网格单元,silverlight,windows-phone-7,Silverlight,Windows Phone 7,我试图在Silverlight中构建一个简单的3x3网格,每个网格单元中都有一个按钮。网格定义如下所示。当我将按钮添加到网格时,它们从不填充130x130网格单元。我将按钮的边距和填充设置为0,并将其水平和垂直对齐设置为拉伸 <Grid x:Name="Test" ShowGridLines="True" HorizontalAlignment="Center"> <Grid.RowDefinitions> <RowD

我试图在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" />

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

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


如果你还没拿到,请告诉我

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

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

如果你还没拿到,请告诉我

试试下面的方法

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

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

现在尝试将按钮封装在网格中。

尝试以下操作:

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

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


现在尝试在网格中封装按钮。

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

Margin="{StaticResource PhoneTouchTargetOverhang}"

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

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

Margin="{StaticResource PhoneTouchTargetOverhang}"

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

试试这个,它不能指定垂直对齐或水平对齐

Margin="-10"

尝试此操作,不能指定垂直对齐或水平对齐

Margin="-10"

您需要提供更完整的XAML视图,如按钮声明…添加一些按钮和样式示例您需要提供更完整的XAML视图,如按钮声明…添加一些按钮和样式示例这是我从这段代码中获得的屏幕截图。你可以看到130像素的网格线,但是按钮并不能填满整个画面。干杯我已经搜索了一段时间的答案,现在从默认情况下填充行/列的WPF网格移动这很烦人,但是你已经用HorizontalAlignment&HorizontalAlignment保存了一天。这是我用这段代码得到的屏幕截图。你可以看到130像素的网格线,但是按钮并不能填满整个画面。干杯我已经搜索了一段时间的答案,现在从默认情况下填充行/列的WPF网格移动这很烦人,但是你已经用HorizontalAlignment&HorizontalAlignment挽救了这一天