这是我正在使用的当前代码:
<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>
水平对齐有效,但verticalalignment不执行任何操作.谢谢你的帮助.
解决方法
问题是通过将字符串分配给ContentPresenter的内容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>
这是按钮的更通用方法.当风格被用于非常特定的本地目的时,当然可以使用更简单的更具描述性的模板.