Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 678983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:12:35+00:00 2026-05-14T01:12:35+00:00

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>

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:12:36+00:00Added an answer on May 14, 2026 at 1:12 am

    The problem is that by assigning string to the Content of the the ContentPresenter Silverlight needs to create a form of TextBlock to present the string. Its the position of this TextBlock which isn’t centered in the vertical space provided by the ContentPresenter. Modifying the button like this:-

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

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

    <Border x:Name="CellBorderBrush" BorderBrush="Black" BorderThickness="1">
        <StackPanel VerticalAlignment="Center"HorizontalAlignment="Center">
            <ContentPresenter Content="{TemplateBinding Content}" />
        </StackPanel>
    </Border>
    

    By placing the ContentPresenter in a StackPanel the ContentPresenter will take the minumum height needed to display its content. The StackPanel in turn only has the height of its content and then it is centered within the Border.

    If I were building this, this is what it would look like:-

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a XAML UserControl that is used to enter the current date
Here is my current question: I'm guessing that my problem (described below) is being
Here's my current SQL statement: SEARCH_ALBUMS_SQL = SELECT * FROM albums WHERE title LIKE
Here is the updated question: the current query is doing something like: $sql1 =
Here is my code, which takes two version identifiers in the form 1, 5,
So in the example code below, I create a UserControl UserControldChild which is a
I am trying to alias a resource in XAML, as follows: <UserControl.Resources> <StaticResourceExtension x:Key=newName
I need a monitor class that regularly checks whether a given HTTP URL is
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.