Click here to Skip to main content
Click here to Skip to main content
Go to top

What is Line Stacking Strategy in Silverlight TextBlock control?

, 17 May 2011
Rate this:
Please Sign up or sign in to vote.
Are you aware of the Line Stacking Strategy of Silverlight TextBlock control? If not, this post will help you to understand it and after that you will be able to use it whenever required.

Are you aware of the Line Stacking Strategy of Silverlight TextBlock control? If not, this post will help you to understand it, and after that you will be able to use it whenever required.

So, what is this strategy? Was it available in earlier versions of Silverlight, or is it a new feature implemented in Silverlight 5? Let us discuss it in depth.

LineStackingStrategy is a property of Silverlight TextBlock control, which takes an enum value that indicates how a line box is determined for each line of text in the TextBlock. It has a default value called System.Windows.LineStackingStrategy.MaxHeight.

So before explaining it in depth with a simple example, you might ask a question "Is it a new feature or an existing feature of Silverlight?" To answer your query, I will say that it's not a new feature. It was available in Silverlight 3, Silverlight 4 and Windows Phone 7. But, it has some additional values in Silverlight 5 to provide you with additional support.

API Difference

Let's explore what is the major difference between Silverlight 5 and its previous versions. Here is the API difference between them:

Enum structure in Silverlight 4

namespace System.Windows
{
    public enum LineStackingStrategy
    {
        MaxHeight,
        BlockLineHeight,
    }
}

Enum structure in Silverlight 5

namespace System.Windows
{
    public enum LineStackingStrategy
    {
        MaxHeight,
        BlockLineHeight,
        BaselineToBaseline,
    }
}

In Silverlight 5, we have a new enum value called "BaselineToBaseline" as shown above, but unfortunately I didn't find any difference between BlockLineHeight and BaselineToBaseline.

Discuss with a Demo

Let us discuss them with a small demo. This will clarify the property and uses to you. Starting from the first one, we will use the same code and will just change the property value. For better clarity, we will use LineHeight="15" here.

MaxHeight

MaxHeight defines the stack height which is the smallest value that contains the extended block progression dimension of all the inline elements on that line when those elements are properly aligned. This is the default. This enum value is part of Silverlight 3, 4 and 5.

<TextBlock Width="500" TextWrapping="Wrap"
           LineHeight="15" LineStackingStrategy="MaxHeight">
    <Run FontSize="30">Lorem Ipsum</Run> 
    is simply dummy text of the printing and typesetting industry. 
    <Run FontSize="20">Lorem Ipsum</Run> has been the industry's standard 
    <Run FontSize="30">dummy text</Run> ever since the 1500s, 
    when an unknown printer took a galley of type and scrambled it 
    to make a type specimen book.
</TextBlock>

Here we added LineStackingStrategy value as "MaxHeight". This is the default value. The rendered text will have a properly aligned text in the same line by its max size. Have a look into the below screenshot for better visibility:

image

BlockLineHeight

BlockLineHeight defines the stack height which is determined by the block element line-height property value. This value is also part of Silverlight 3, 4 and 5.

<TextBlock Width="500" TextWrapping="Wrap"
           LineHeight="15" LineStackingStrategy="BlockLineHeight">
    <Run FontSize="30">Lorem Ipsum</Run> 
    is simply dummy text of the printing and typesetting industry. 
    <Run FontSize="20">Lorem Ipsum</Run> has been the industry's standard 
    <Run FontSize="30">dummy text</Run> ever since the 1500s, 
    when an unknown printer took a galley of type and scrambled it 
    to make a type specimen book.
</TextBlock>

The code here uses BlockLineHeight as the LineStackingStrategy. Here it will use the same block height over the entire text, which will look as below:

image

BaselineToBaseline

BaselineToBaseline defines the stack height which is determined by adding LineHeight to the baseline of the previous line. This enum value is part of only Silverlight 5.

<TextBlock Width="500" TextWrapping="Wrap"
           LineHeight="15" LineStackingStrategy="BaselineToBaseline">
    <Run FontSize="30">Lorem Ipsum</Run> 
    is simply dummy text of the printing and typesetting industry. 
    <Run FontSize="20">Lorem Ipsum</Run> has been the industry's standard 
    <Run FontSize="30">dummy text</Run> ever since the 1500s, 
    when an unknown printer took a galley of type and scrambled it 
    to make a type specimen book.
</TextBlock>

Here we provided the LineStackingStrategy value as BaselineToBaseline. The rendered text didn't produce any difference there. Have a look at it here:

image

As I mentioned, I didn't find any difference between BlockLineHeight and BaselineToBaseline values. If you know the difference with a proper sample, please let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Generaldifference b/w Baseline2Baseline & BlockLineHeight LineStatckingStrategy PinmemberMember 1030002628-Oct-13 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 17 May 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid