question

58901228 avatar image
0 Votes"
58901228 asked 58901228 edited

How to get the actual width of TextBlock?

I defined a TextBlock object in the form and set a string for it. Now I want to get its actual pixel width. When I use the following method, cs0618 warning appears. Is there any other way to get it?

 var fromatted = new FormattedText(
                         text.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                         new Typeface(text.FontFamily, text.FontStyle, text.FontWeight, text.FontStretch),
                         text.FontSize, text.Foreground);
windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why do you want to put FormattedText in the TextBlock? You've all relevant Properties in the TextBlock to format the text. The only time I've used FormattedText when I added text dynamically OnRender and in that case you don't need any TextBlock, you just put the FormattedText in the desired coordinate and it acquires the required space to render text.

0 Votes 0 ·
DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered 58901228 edited

Solution: Please add ,VisualTreeHelper.GetDpi(this).PixelsPerDip after your text.Foreground.

Explanation: You use the Obsolete FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush), which need to use PixelsPerDip to override , you could refer to official document: FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush) for more details. Its corresponding new method is FormattedText(String, CultureInfo, FlowDirection, Typeface, Double, Brush, Double)

Updated code:

 var fromatted = new FormattedText(
                          text.Text, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight,
                          new Typeface(text.FontFamily, text.FontStyle, text.FontWeight, text.FontStretch),
                          text.FontSize, text.Foreground, VisualTreeHelper.GetDpi(this).PixelsPerDip);

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you you answer, this just i want.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered 58901228 commented

See the following. If these do not help then your question is not clear.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

In fact, before TextBlock is put into the window, I need to create a TextBlock object through code and add a string for its text attribute. Then I need to calculate the pixel width of this TextBlock, so as to calculate where it should be put.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered 58901228 commented

If you need a text block that is resized automatically according to text, then check an example:

 <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50,50,0,0" Text="Some text" />

Put it inside a <Grid>, for example. You do not have to calculate and set the width.

I think that such textblock can be inserted programmatically too; then you can get its actual width and reposition it. But maybe you should avoid explicit positioning using the corresponding alignment and margin properties.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you you answer,but is not i want.

0 Votes 0 ·