How to make wpf textblock autosize

I have a text block where I dynamically add a line. even if I add the line width and update the text block, the text block does not display the corresponding width, still some text is cropped.

How to measure the width that should be displayed in a text block? and how to make it automatic?

+4
source share
4 answers

You can get the text size using the following solutions:

Solution 1

You can format a Text to measure text size, here is an example:

String text = "Here is my text"; Typeface myTypeface = new Typeface("Helvetica"); FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, myTypeface, 16, Brushes.Red); Size textSize = new Size(ft.Width, ft.Height); 

Decision 2

Use the Graphics class (found here ):

 System.Drawing.Font font = new System.Drawing.Font("Calibri", 12, FontStyle.Bold); Bitmap bitmap = new Bitmap(1, 1); Graphics g = Graphics.FromImage(bitmap); SizeF measureString = g.MeasureString(text, font); 

There you are!

+6
source

Text blocks that do not have a specified height or width will automatically expand until they fill their container. So try this one.

+3
source

Here the class I wrote does this. This is only AutoShrinks, but you can add the necessary features. It uses the parent boundaries of OR MaxHeight / MaxWidth, if set.

 public class TextBlockAutoShrink : TextBlock { // private Viewbox _viewBox; private double _defaultMargin = 6; private Typeface _typeface; static TextBlockAutoShrink() { TextBlock.TextProperty.OverrideMetadata(typeof(TextBlockAutoShrink), new FrameworkPropertyMetadata(new PropertyChangedCallback(TextPropertyChanged))); } public TextBlockAutoShrink() : base() { _typeface = new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch, this.FontFamily); base.DataContextChanged += new DependencyPropertyChangedEventHandler(TextBlockAutoShrink_DataContextChanged); } private static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var t = sender as TextBlockAutoShrink; if (t != null) { t.FitSize(); } } void TextBlockAutoShrink_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { FitSize(); } protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) { FitSize(); base.OnRenderSizeChanged(sizeInfo); } private void FitSize() { FrameworkElement parent = this.Parent as FrameworkElement; if (parent != null) { var targetWidthSize = this.FontSize; var targetHeightSize = this.FontSize; var maxWidth = double.IsInfinity(this.MaxWidth) ? parent.ActualWidth : this.MaxWidth; var maxHeight = double.IsInfinity(this.MaxHeight) ? parent.ActualHeight : this.MaxHeight; if (this.ActualWidth > maxWidth) { targetWidthSize = (double)(this.FontSize * (maxWidth / (this.ActualWidth + _defaultMargin))); } if (this.ActualHeight > maxHeight) { var ratio = maxHeight / (this.ActualHeight); // Normalize due to Height miscalculation. We do it step by step repeatedly until the requested height is reached. Once the fontsize is changed, this event is re-raised // And the ActualHeight is lowered a bit more until it doesnt enter the enclosing If block. ratio = (1 - ratio > 0.04) ? Math.Sqrt(ratio) : ratio; targetHeightSize = (double)(this.FontSize * ratio); } this.FontSize = Math.Min(targetWidthSize, targetHeightSize); } } } 
+2
source

Here is another way to solve this problem.

Set yourTextBlock.Width = double.NaN and the same with Height .

+1
source

All Articles