locked
How to give multiline in textblock: RRS feed

  • Question

  • Hi,

    I want to display text in multilines in texblock. Not the wraping. In a grid, I've the textblock. The text in the textblock should be displayed in the multilines. Basically I should be able to give number of lines example no of lines to be 3. If only one text is given it should be displayed in line 1, 2nd text in line 2 and so on.

    Please do let me know if I  can do this in XAML. If yes, how?

    Kindly waiting for the response.

    Thanks,

    Santosh

    Thursday, April 16, 2015 10:47 AM

Answers

  • I don't know if I understand your issue but if you want to add linebreaks to a TextBlock you could use the LineBreak element:

            <TextBlock>
                <Run Text="here is line 1"/>
                <LineBreak/>
                <Run Text="here is line 2"/>
                <LineBreak/>
                <Run Text="here is line 3"/>
            </TextBlock>

    ...or an encoded newline character:

     <TextBlock Text="line1&#x0a;line2" />

    ..or you could put different TextBlocks in a StackPanel:

    <StackPanel>
    <TextBlock Text="line1..."/>
    <TextBlock Text="line2..."/>
    </StackPanel>

    If you just set the Text property of the TextBlock to a long string without any newline characters in it, the actual height and width, the wrapping and the font size etc. of the TextBlock will decide how many lines you get. There is no property that you can use to specify that it should always contain x number of lines if that's what you are looking for.

    So you should define the linebreaks yourself or set some constraint on the size of the TextBlock and/or the font size of the text in it to be able to get a specific number of lines.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    Thursday, April 16, 2015 1:39 PM

All replies

  • You can always tell WPF to preserve the whitespace in the content (though it preserves all whitespace so it can look a bit odd if you set the text directly in the XAML):

    <TextBlock xml:space="preserve">Hello
    world</TextBlock>

    Thursday, April 16, 2015 11:45 AM
  • I don't know if I understand your issue but if you want to add linebreaks to a TextBlock you could use the LineBreak element:

            <TextBlock>
                <Run Text="here is line 1"/>
                <LineBreak/>
                <Run Text="here is line 2"/>
                <LineBreak/>
                <Run Text="here is line 3"/>
            </TextBlock>

    ...or an encoded newline character:

     <TextBlock Text="line1&#x0a;line2" />

    ..or you could put different TextBlocks in a StackPanel:

    <StackPanel>
    <TextBlock Text="line1..."/>
    <TextBlock Text="line2..."/>
    </StackPanel>

    If you just set the Text property of the TextBlock to a long string without any newline characters in it, the actual height and width, the wrapping and the font size etc. of the TextBlock will decide how many lines you get. There is no property that you can use to specify that it should always contain x number of lines if that's what you are looking for.

    So you should define the linebreaks yourself or set some constraint on the size of the TextBlock and/or the font size of the text in it to be able to get a specific number of lines.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    Thursday, April 16, 2015 1:39 PM
  • TextBlock is a contentcontrol.

    Which means ( perhaps counterintuitively ) you can put an itemscontrol inside it and bind a collection.

    You would need a list<t> or observablecollection<t> as a public property in your viewmodel.

    Call that LineCollection.

    You can then bind:

            <TextBlock>
                <ItemsControl ItemsSource="{LineCollection}">
                    <TextBlock Text="{Binding aLine}"/>
                </ItemsControl>
            </TextBlock>

    Or you could of course just use an itemscontrol without the textblock wrapping it, depending on your requirement.

    PS

    I just realised I forgot the itemtemplate, bit late but maybe someone else will read this.


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles



    Friday, April 17, 2015 7:43 AM