Here is the current code I am using:
<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>
The Horizontal aligning works but the verticalalignment doesn’t do anything. Thanks for your help.
The problem is that by assigning string to the
Contentof the theContentPresenterSilverlight needs to create a form ofTextBlockto present the string. Its the position of thisTextBlockwhich isn’t centered in the vertical space provided by theContentPresenter. Modifying the button like this:-would fix it. However you might just want to be able to specifiy a simple string Content and have it centered. In that case you could modify your template:-
By placing the
ContentPresenterin aStackPaneltheContentPresenterwill take the minumum height needed to display its content. TheStackPanelin turn only has the height of its content and then it is centered within theBorder.If I were building this, this is what it would look like:-
This is a more general approach to the button. Of course its fine to use the simpler more perscriptive templates when the style is being used for a very specific local purposes.