
<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> 这是按钮的更通用方法.当风格被用于非常特定的本地目的时,当然可以使用更简单的更具描述性的模板.
总结以上是内存溢出为你收集整理的如何对齐文本,使其位于Silverlight中的按钮中间?全部内容,希望文章能够帮你解决如何对齐文本,使其位于Silverlight中的按钮中间?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)