这是我正在使用的当前代码:
<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>
Run Code Online (Sandbox Code Playgroud)
水平对齐有效,但verticalalignment不执行任何操作.谢谢你的帮助.
Ant*_*nes 14
问题是通过Content为ContentPresenterSilverlight 分配字符串需要创建一个TextBlock表示字符串的形式.它的位置TextBlock不在于由垂直空间提供的中心ContentPresenter.像这样修改按钮: -
<Button Style="{StaticResource CellStyle}">
<TextBlock Text="A" VertialAlignment="Center" />
</Button>
Run Code Online (Sandbox Code Playgroud)
会解决它.但是,您可能只希望能够指定一个简单的字符串Content并使其居中.在这种情况下,您可以修改您的模板: -
<Border x:Name="CellBorderBrush" BorderBrush="Black" BorderThickness="1">
<StackPanel VerticalAlignment="Center"HorizontalAlignment="Center">
<ContentPresenter Content="{TemplateBinding Content}" />
</StackPanel>
</Border>
Run Code Online (Sandbox Code Playgroud)
通过将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>
Run Code Online (Sandbox Code Playgroud)
这是按钮的更通用方法.当风格被用于非常特定的本地目的时,当然可以使用更简单的更具描述性的模板.
小智 8
我正在使用的东西可以帮助你的事业:
<Button Content="A" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
Run Code Online (Sandbox Code Playgroud)