如何在Silverlight中使文本居中显示在按钮中间?

8

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

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

水平对齐可以正常工作,但是垂直对齐没有任何作用。感谢您的帮助。

2个回答

15

问题在于将字符串分配给Silverlight的ContentPresenterContent时,需要创建一种TextBlock形式来表示该字符串。这个TextBlock的位置没有在由ContentPresenter提供的垂直空间中心对齐。将按钮修改为以下内容即可解决问题:

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

你可以修复它。但是你可能只想指定一个简单的字符串内容并使其居中。在这种情况下,你可以修改你的模板:

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

这是一个更通用的按钮方法。当样式仅用于非常具体的本地目的时,使用简单更规范的模板也可以。


8
我使用的工具也许能帮助你的事业:
<Button Content="A" HorizontalContentAlignment="Center"  VerticalContentAlignment="Center"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接