Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the text in one line in wpf textblock

Tags:

wpf

textblock

I'm a newbie with wpf , what i want to display the text in one line in wpf textblock. eg.:

<TextBlock 
    Text ="asfasfasfa
    asdasdasd"
</TextBlock>

TextBlock display it in two lines default,

but i want it in only one line like this"asafsf asfafaf". I mean show all the text in one line even there are more than one lines in the text
what should i do?

like image 906
baorui Avatar asked Jan 22 '10 06:01

baorui


People also ask

How do I create a line break in TextBlock WPF?

Adding Line Breaks Sometimes you will want to insert a line break within a TextBlock. You can do this with a LineBreak inline, added as an XAML element within the text. A new line will be started at the position of this element.

How do you add a line in WPF?

To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.

What is the difference between TextBlock and label in WPF?

Labels usually support single line text output while the TextBlock is intended for multiline text display. For example in wpf TextBlock has a property TextWrapping which enables multiline input; Label does not have this.


2 Answers

Use a Converter:

    <TextBlock Text={Binding Path=TextPropertyName,
Converter={StaticResource SingleLineTextConverter}}

SingleLineTextConverter.cs:

public class SingleLineTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = (string)value;
        s = s.Replace(Environment.NewLine, " ");
        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
like image 195
Kishore Kumar Avatar answered Oct 23 '22 15:10

Kishore Kumar


Instead of this:

            <TextBlock Text="Hello
                How Are
                You??"/>

Use this:

            <TextBlock>
                Hello
                How Are
                You??
            </TextBlock>

or this:

            <TextBlock>
                <Run>Hello</Run> 
                <Run>How Are</Run> 
                <Run>You??</Run>
            </TextBlock>

or set Text property in code behind like this :

(In XAML)

            <TextBlock x:Name="MyTextBlock"/>

(In code - c#)

            MyTextBlock.Text = "Hello How Are You??"

Code-behind approach has an advantage that you can format your text before setting it. Example: If the text is retrieved from a file and you want to remove any carriage-return new-line characters you can do it this way:

 string textFromFile = System.IO.File.ReadAllText(@"Path\To\Text\File.txt");
 MyTextBlock.Text = textFromFile.Replace("\n","").Replace("\r","");
like image 37
mg007 Avatar answered Oct 23 '22 17:10

mg007