WPF Textblock, salto de línea en el atributo de texto


¿Hay una manera de hacer que \n haga un salto de línea en un TextBlock?

<TextBlock Text="line1\nLine2" />

¿O hay una mejor manera de forzar un salto de línea intermedia, dentro del atributo Text?

<LineBreak />

Esto no funciona para mí, necesita ser el valor del atributo Text, porque la cadena de texto se establece desde una fuente externa.

Estoy familiarizado con LineBreak pero no es la respuesta que estoy buscando.

Author: H. Pauwelyn, 2009-05-08

12 answers

Sé que esto está volviendo a corregir una vieja pregunta, pero yo tenía el mismo problema. La solución para mí fue usar fuentes de línea codificadas HTML (&amp;#10;).

Line1&amp;#10;Line2

Parece

Line1
Line2

Para obtener más de los caracteres codificados en HTML, consulte w3schools

 104
Author: Noaki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-16 14:38:08

Prueba esto:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>
 108
Author: Paul Alexander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-15 22:01:36

La forma más fácil es

<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>

Así que simplemente escribes código XAML, y el <LineBreak /> tiene exactamente el mismo significado que el
en HTML o el "\n" en C#.

 10
Author: Stephane Halimi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-24 05:06:31
 6
Author: jcollum,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-05-07 22:23:40

¿Qué tal si dividimos la línea en dos etiquetas?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>
 5
Author: Cameron MacFarland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-05-08 00:13:23

no funcionará si está dentro de una colección como Grid o StackPanel. En tales casos, lo siguiente funcionaría como se muestra:

Salto de línea dentro de una colección

 3
Author: user2063329,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-29 22:12:59

La forma correcta de usarlo puede ser la siguiente:

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>
 2
Author: radu florescu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-19 07:02:35
  <HyperlinkButton 
        Content="Apply and restart this pplication!&#10;&#13;Note that modifying these settings requires the application to be restarted."   />

CRLF simple way = !&#10;&#13;

!&#10;&#13; - Trabaja en todos los controles wpf, xaml, silverlight como TextBlock, HyperlinkText y más

 1
Author: user1551704,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-31 13:12:10

Simplemente use el control AccessText. puede usarlo como una etiqueta y tiene la propiedad TextWrapping="WrapWithOverflow"

Eg.

El mío es así y funciona bien. Además, no tiene ningún problema en cambiar el texto dinámicamente.

 1
Author: MelloG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-04 12:58:53

Si está vinculando el Texto de TextBlock, ninguna de las otras respuestas funciona. Simplemente agregue '\n ' al texto de enlace donde desea romper.

 1
Author: newman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-10 20:43:20

Llego tarde a la fiesta pero .. esto es más o menos cómo lo hice, (tenga en cuenta que mis ItemSources son cadenas simples , no formateadas, y no necesitaba 'ConvertBack' nada)

public class SpaceToLineBreakConverter : IValueConverter
{   
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {            
        return (!String.IsNullOrEmpty(value as string)) 
        ? new Regex(@"\s").Replace(value as string, "\n") 
        : value;            
    }

    public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
 1
Author: Dan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-26 06:46:44

Estaba teniendo un problema similar y quería enlazar una cadena de marcado xaml a un TextBlock. Esencialmente almacenando el marcado declarativo dentro de un TextBlock en una cadena para su uso posterior.

Así es como lo hice: subclasé el TextBlock para hacer que la InlineCollection sea enlazable y escribí un Convertidor entre la cadena y una InlineCollection(o en realidad una lista genérica de Inlines.)

 0
Author: Jodrell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:23