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 559691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:15:51+00:00 2026-05-13T12:15:51+00:00

The following snippet: <Window x:Class=Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <Grid> <StackPanel Orientation=Horizontal VerticalAlignment=Center HorizontalAlignment=Center> <Label Content=Name:/>

  • 0

The following snippet:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <StackPanel Orientation="Horizontal"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center">            
            <Label Content="Name:"/>
            <Label Content="Itzhak Perlman" FontSize="44"/>
        </StackPanel>
    </Grid>
</Window>

Renders the following:
alt text

Is there any way I can set in the Labels’ styles so that their text bottoms should be aligned?
I have the same question with TextBlocks as well.

NOTE: since I’ve been struggling with this issue for a while, please post only certains answers that you know that work.
I already tried: VerticalAlignment, VerticalContentAlignment, Padding, Margin. Is there anything else I am not aware of?

I’ve read this post, but it doesn’t talk about a scenario of different font size.

UPDATE: The problem is, that even Padding is set to 0 there is still an indeterminate space around the font, within the ContentPresenter area. this space varies on the font size.
If I could control this space I would be in a better situation.

Thanks

  • 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-13T12:15:52+00:00Added an answer on May 13, 2026 at 12:15 pm

    There is no XAML only solution, you have to use code behind. Also, even with code-behind, there’s no general solution for this, because what if your text is multi-line? Which baseline should be used in that case? Or what if there are multiple text elements in your template? Such as a header and a content, or more, which baseline then?

    In short, your best bet is to align the text manually using top/bottom margins.

    If you’re willing to make the assumption that you have a single text element, you can figure out the pixel distance of the baseline from the top of the element by instantiating a FormattedText object with all the same properties of the existing text element. The FormattedText object has a double Baseline property which holds that value. Note that you still would have to manually enter a margin, because the element might not sit exactly against the top or bottom of its container.

    See this MSDN forum post: Textbox Baseline

    Here’s a method I wrote that extracts that value. It uses reflection to get the relevant properties because they are not common to any single base class (they are defined separately on Control, TextBlock, Page, TextElement and maybe others).

    public double CalculateBaseline(object textObject)
    {
        double r = double.NaN;
        if (textObject == null) return r;
    
        Type t = textObject.GetType();
        BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
    
        var fontSizeFI = t.GetProperty("FontSize", bindingFlags);
        if (fontSizeFI == null) return r;
        var fontFamilyFI = t.GetProperty("FontFamily", bindingFlags);
        var fontStyleFI = t.GetProperty("FontStyle", bindingFlags);
        var fontWeightFI = t.GetProperty("FontWeight", bindingFlags);
        var fontStretchFI = t.GetProperty("FontStretch", bindingFlags);
    
        var fontSize = (double)fontSizeFI.GetValue(textObject, null);
        var fontFamily = (FontFamily)fontFamilyFI.GetValue(textObject, null);
        var fontStyle = (FontStyle)fontStyleFI.GetValue(textObject, null);
        var fontWeight = (FontWeight)fontWeightFI.GetValue(textObject, null);
        var fontStretch = (FontStretch)fontStretchFI.GetValue(textObject, null);
    
        var typeFace = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
    
        var formattedText = new FormattedText(
            "W", 
            CultureInfo.CurrentCulture, 
            FlowDirection.LeftToRight, 
            typeFace, 
            fontSize, 
            Brushes.Black);
    
        r = formattedText.Baseline;
    
        return r;
    }
    

    EDIT: Shimmy, in response to your comment, I don’t believe you’ve actually tried this solution, because it works. Here’s an example:

    Example Baseline Alignment

    Here’s the XAML:

    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="0,40,0,0"/>
            </Style>
        </StackPanel.Resources>
        <StackPanel Orientation="Horizontal">
            <TextBlock Name="tb1" Text="Lorem " FontSize="10"/>
            <TextBlock Name="tbref" Text="ipsum"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Name="tb2" Text="dolor "  FontSize="20"/>
            <TextBlock Text="sit"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Name="tb3" Text="amet "  FontSize="30"/>
            <TextBlock Text="consectetuer"/>
        </StackPanel>
    </StackPanel>
    

    And here’s the code behind that achieves this

    double baseRef = CalculateBaseline(tbref);
    double base1 = CalculateBaseline(tb1) - baseRef;
    double base2 = CalculateBaseline(tb2) - baseRef;
    double base3 = CalculateBaseline(tb3) - baseRef;
    tb1.Margin = new Thickness(0, 40 - base1, 0, 0);
    tb2.Margin = new Thickness(0, 40 - base2, 0, 0);
    tb3.Margin = new Thickness(0, 40 - base3, 0, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following snippet: public class a { public void otherMethod(){} public void doStuff(String
I have the following snippet in one of my html pages : <div class=inputboximage>
I have the following HTML snippet: <body> <div class=main> <div class=topBar> <p>testing</p> </div> <div
Consider the following snippet: var t = <span>Hello world</span>; var range = window.getSelection().getRangeAt(0); range.deleteContents();
I saw the following code snippet: class WindowHandle { public: WindowHandle(WINDOW_HANDLE handle) : w(handle)
The following snippet of C# code: int i = 1; string result = String.Format({0},{1},{2},
The following snippet is supposed to take the value of PROJECT (defined in the
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
In the following snippet, using XmlReader, when I encounter an element. I would like
I have the following snippet of code, changeTextArea is a TextArea object. changeTextArea.addKeyboardListener(new KeyboardListenerAdapter()

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.