这是我正在使用的当前代码:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ButtonPrototype.MainPage"
    Width="640" Height="480">
    <UserControl.Resources>
        <ControlTemplate x:Key="CellTemplate" TargetType="Button">
            <Grid>
                <Border x:Name="CellBorderBrush" BorderBrush="Black" BorderThickness="1">
                    <ContentPresenter
                      Content="{TemplateBinding Content}"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"/>
                </Border>
            </Grid>
        </ControlTemplate>

        <Style x:Key="CellStyle" TargetType="Button">
            <Setter Property="Template" Value="{StaticResource CellTemplate}"></Setter>
            <Setter Property="Foreground" Value="Black"></Setter>
            <Setter Property="FontSize" Value="80"></Setter>
            <Setter Property="Width" Value="100"></Setter>
            <Setter Property="Height" Value="100"></Setter>
        </Style>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="A" Style="{StaticResource CellStyle}"></Button>
    </Grid>
</UserControl>

水平对齐有效,但垂直对齐没有任何作用。谢谢你的帮助。

最佳答案

问题在于,通过将字符串分配给ContentContentPresenter,Silverlight需要创建一种TextBlock的形式来呈现该字符串。该TextBlock的位置,该位置不在ContentPresenter提供的垂直空间的中心。像这样修改按钮:

<Button Style="{StaticResource CellStyle}">
   <TextBlock Text="A" VertialAlignment="Center" />
</Button>

会解决它。但是,您可能只希望能够指定一个简单的字符串Content并将其居中。在这种情况下,您可以修改模板:
<Border x:Name="CellBorderBrush" BorderBrush="Black" BorderThickness="1">
    <StackPanel VerticalAlignment="Center"HorizontalAlignment="Center">
        <ContentPresenter Content="{TemplateBinding Content}" />
    </StackPanel>
</Border>

通过将ContentPresenter放置在StackPanel中,ContentPresenter将采用显示其内容所需的最小高度。 StackPanel依次仅具有其内容的高度,然后位于Border的中心。

如果我正在构建它,则它看起来将是这样的:-
<UserControl x:Class="SilverlightApplication1.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
    <UserControl.Resources>
        <ControlTemplate x:Key="CellTemplate" TargetType="Button">
            <Grid>
                <Border x:Name="CellBorderBrush"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}" />
                <ContentPresenter
                      x:Name="contentPresenter"
                      Content="{TemplateBinding Content}"
                      ContentTemplate="{TemplateBinding ContentTemplate}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      Margin="{TemplateBinding Padding}"/>
            </Grid>
        </ControlTemplate>
        <Style x:Key="CellStyle" TargetType="Button">
            <Setter Property="Template" Value="{StaticResource CellTemplate}" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="80" />
            <Setter Property="Width" Value="100" />
            <Setter Property="Height" Value="100" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Padding" Value="1" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Style="{StaticResource CellStyle}">
            <TextBlock Text="A" VerticalAlignment="Center" />
        </Button>
    </Grid>
</UserControl>

这是一种更通用的按钮方法。当然,当样式用于非常特定的本地目的时,可以使用更简单,更具说明性的模板。

10-04 12:03