How to display the text in one line in wpf textblock

baorui  picture baorui · Jan 22, 2010 · Viewed 13.1k times · Source

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?

Answer

Kishore Kumar picture Kishore Kumar · Jan 22, 2010

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();
    }
}