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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:07:41+00:00 2026-05-22T21:07:41+00:00

I know we can stretch item to the right or down using ColumnSpan and

  • 0

I know we can stretch item to the right or down using ColumnSpan and RowSpan

However I got an Item I want to display in grid cell and have it centered according to this cell center, so it will span both right and left in case of text, here I provide an image of how it currently looks, “12345” text can’t fit the column and is displayed as “234”, is there a way to make 1 & 5 visible leaving all columns untouched?enter image description here

Text can be changed to anything and has to be centered correctly, I can hack a way to display 12345 this way , but need a solution that will work with any text, just in case,and textBlock grid.Column has to be left untouched 1 (or 2 to be precise for my case) , not 0 for this picture case.

  • 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-22T21:07:42+00:00Added an answer on May 22, 2026 at 9:07 pm

    Solution 4
    So what you actually want is a way to center the text dynamically if I now understand correctly. You don’t want to have to work out fiddling with the padding yourself, you want a number that works for everything? I sure hope you appreciate this! 🙂

    /// <summary>
    /// Takes a FrameworkElement that is spanned across
    /// multiple columns and RETURNS a Thickness that
    /// can be applied to the Padding property to
    /// centre it across the spanned columns
    /// </summary>
    /// <param name="fe">The FrameworkElement to be centred</param>
    /// <param name="centerColumn">The index of the desired centre column</param>
    /// <returns>A Thickness that will center the FrameworkElement</returns>
    private Thickness GetCenteringPadding(FrameworkElement fe, int centerColumn)
    {
        Grid parentGrid = fe.Parent as Grid;
        if (parentGrid == null)
            throw new ArgumentException();
    
        // Variables
        int firstColumn = (int)fe.GetValue(Grid.ColumnProperty);
        int columnSpan = (int)fe.GetValue(Grid.ColumnSpanProperty);
        double totalWidth = 0.0;
        double leftWidth = 0.0;
        double leftPaddingWidth = 0.0;
    
        // Total the width for all the spanned columns
        for (int i = firstColumn; i < columnSpan + firstColumn; i++)
        {
            // This part can be dangerous, especially if you're using a '*'
            totalWidth += parentGrid.ColumnDefinitions[i].ActualWidth;
        }
    
        // Get the total width from the left side of the first
        //  spanned column, to the center of the 'centerColumn'
        for (int j = firstColumn; j <= centerColumn; j++)
        {
            if (j != centerColumn)
                leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth;
            else // Only take half the width for the center column
                leftWidth += parentGrid.ColumnDefinitions[j].ActualWidth / 2;
        }
    
        // Calculate the padding width
        // (Abbr. rightWidth = tW - lW, lPW = lW - rW)
        leftPaddingWidth = 2*leftWidth - totalWidth;
    
        // Check whether the padding needs to be on the left or the right
        if (leftPaddingWidth > 0.0)
        {
            // The excess space is on the left
            return new Thickness(leftPaddingWidth, 0.0, 0.0, 0.0);
        }
        else
        {
            // The excess space is on the right
            return new Thickness(0.0, 0.0, -leftPaddingWidth, 0.0);
        }
    }
    

    A couple of notes. It uses the .ActualWidth of the columns in the Grid. You can change this of course to .Width, however you may run into problems when you’re using ColumnDefinition Width="30*" because I’m quite sure you won’t get a double as the result then. Secondly, this function doesn’t set the padding on the object you pass for two reasons:
    – You can take the returned Thickness and add it to the .Padding already applied to a Control so that you don’t destroy it’s formatting.
    – You can pass any FrameworkElement, whereas only Controls have padding. Perhaps you could add the returned thickness to the .Margin property instead, or even create a container and apply padding within that?
    Finally, you could easily combine the two for loops within the function, but I left them seperate from clarity.
    Cheers.

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

Sidebar

Related Questions

I have a query that I know can be done using a subselect, but
I know I can do most of this by hacking Trac and using Git
I know I can programatically make the taskbar item for a particular window start
Does anyone know how I can stretch a surface view beyond the visible bounds
This is something I know can be done somehow , because I've done it
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know you can look at the row.count or tables.count, but are there other
I know you can do redirection based on the domain or path to rewrite
I know I can call the GetVersionEx Win32 API function to retrieve the Windows
I know I can figure out the name of the method as its being

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.