Menu
How do I make a WPF TextBlock show my text on multiple lines? Ask Question Asked 9 years, 2 months ago. Active 2 years, 4 months ago. Viewed 119k times 54. I have a wpf window where i have a stackpanel with two viewports in it - each Viewport with a textblock in it. This example shows how to use Extensible Application Markup Language (XAML) to define a TextBox control that will automatically expand to accommodate multiple lines of text. Example Setting the TextWrapping attribute to Wrap will cause entered text to wrap to a new line when the edge of the TextBox control is reached, automatically expanding. The TextBlock control is one of the most fundamental controls in WPF, yet it's very useful. It allows you to put text on the screen, much like a Label control does, but in a simpler and less resource demanding way.
Today I am going to share a interesting learning experience that I had about editable combo box. In one of my project we have a requirement that all editable text need to support multi-line support. very easy stuff. Just set IsEditable property of combo box to 'true'.
Now, the problem is, in couple of our grid cell we had combo box with editable feature. User can write their own choice or they can pick a value from the drop down list. since the text box of combo is a text editing area. clients wanted warp text with new line in the edit area of the combo box. After few minutes of 'R & D' we discovered the way to override the control template and modified the textbox of combo box in such a way so that it can take multi-line and warp text content in combo box.
To give a textbox warped text feature i just added the following code, and it will do the job.
Wpf Textblock Multiline Line Spacing
Best of luck and happy programming.
Render just the first line of a TextBlock
Thomas Claudius HuberSometimes you want a TextBlock to just display one line. For example if you have a huge virtualized ListView that contains many data items and in the DataTemplate you’re using a TextBlock. When the TextBlock has different height, the virtualization leads to some not so nice behavior when scrolling:
The Thumb of the ScrollBar changes its size while scrolling, as the final extend size is not known, as the items in the ListView have different sizes because of multiple lines.
So when you have virtualization, you might want to display your multiline messages in a master-detail-way. Select a single-line-item in the ListView and display the multiline message somewhere else.
But now the problem is, how to tell the TextBlock to render just the first line? Note that I want to adjust the rendering. I don’t want to adjust the strings with a converter or whatever. Just the rendering! :-)
The TextBox has for the single-line-scenario a MaxLines-Property. But the TextBlock does not have this property.
The TextBlock also does not allow to override its MeasureOverride-method to measure the line height and do custom stuff, as this method is marked as sealed in the TextBlock class.
The TextBlock also does not allow to override its MeasureOverride-method to measure the line height and do custom stuff, as this method is marked as sealed in the TextBlock class.
Wpf Multiline Label
So how could you display only the first line of a TextBlock like this:
To render just the first line, I use a little trick. I place the TextBlock from above together with a hidden TextBlock with just one line in a Grid. Then I can bind the real TextBlock’s Height to the ActualHeight of the hidden one-line-TextBlock. Here the XAML-code, works like a charm.
Note that you could place the hidden one-liner-TextBlock also somewhere else, especially if you have thousands of records that would use a TextBlock via a DataTemplate