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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:04:31+00:00 2026-05-19T15:04:31+00:00

I need to display words on a WPF Canvas in such a way that

  • 0

I need to display words on a WPF Canvas in such a way that they perfectly fit in pre-defined boxes.

One box typically contains a single line of text, from one letter to a few words.

The text inside a box must be as large as possible, i.e: touching all borders of the box (except maybe where it would cause too much text distortion due to abnormal box witdh/height ratio).

I could not find a good way to calculate the appropriate font height, scaling and offset, based on the text content.

A first solution where the original text width/height ratio can’t be changed would already be very nice !

I’d like to use TextBlock elements, but anything else that works should be ok.

  • 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-19T15:04:32+00:00Added an answer on May 19, 2026 at 3:04 pm

    As the answer by Robery Levy said, you can use a Viewbox to achieve this. The text itself won’t stretch however so you’ll still have some “margin” on zero or more sides depending on your text (as you noticed). To work around this you can create a custom control which builds a Geometry from a FormattedText and then draw this with DrawGeometry in OnRender. You’ll notice how the quality of the text improves with a larger FontSize. A very small text (e.g. FontSize="10") won’t look very sharp in a large Viewbox so you’ll have to experiment a bit

    enter image description here

    Some sample Xaml

    <Canvas Background="Black">
        <Viewbox Canvas.Left="10" Canvas.Top="10"
                 Stretch="Fill" Width="200" Height="50">
            <Border Background="Red">
                <local:StretchText Text="Text" Foreground="Green" FontSize="100"/>
            </Border>
        </Viewbox>
        <Viewbox Canvas.Left="230" Canvas.Top="10"
                 Stretch="Fill" Width="200" Height="50">
            <Border Background="Red">
                <local:StretchText Text="B" Foreground="Green" FontSize="500"/>
            </Border>
        </Viewbox>
    </Canvas>
    

    StretchText.cs

    public class StretchText : Control
    {
        protected override void OnRender(DrawingContext drawingContext)
        {
            FormattedText formattedText = new FormattedText(
                Text,
                CultureInfo.GetCultureInfo("en-us"),
                FlowDirection.LeftToRight,
                new Typeface(FontFamily, FontStyle, FontWeight, FontStretches.Normal),
                FontSize,
                Foreground);
    
            Geometry textGeometry = formattedText.BuildGeometry(new Point(0, 0));
            this.MinWidth = textGeometry.Bounds.Width;
            this.MinHeight = textGeometry.Bounds.Height;
    
            TranslateTransform translateTransform = new TranslateTransform();
            translateTransform.X = -textGeometry.Bounds.Left;
            translateTransform.Y = -textGeometry.Bounds.Top;
            drawingContext.PushTransform(translateTransform);
            drawingContext.DrawGeometry(Foreground, new Pen(Foreground, 1.0), textGeometry);
        }
    
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text",
            typeof(string),
            typeof(StretchText),
            new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am developing one application with map view i need display the weather depends
I need to display image and one button on fancybox popup but I can't
I need to display all the employees in one webpart. I created the gridview
I need to display a banner that sticks to the bottom of the browser.
I need to create a toggle that animates upwards not downwards in other words
I need to format numbers (using WPF converters), and the only way I can
I need to display some media in a WPF project. The media could be
Using the WPF DataGrid I have the need to change various display and related
I need to display short description like label and near it data associated with
I need to display a multiply sign (×) in a modal being created by

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.