developer tip

WPF Textblock, Text 속성의 줄 바꿈

optionbox 2020. 9. 20. 09:30
반응형

WPF Textblock, Text 속성의 줄 바꿈


이 할 수있는 방법이 있나요 \nA의 줄 바꿈을은 TextBlock?

<TextBlock Text="line1\nLine2" />

아니면 Text속성 내부에서 중간 줄 바꿈을 강제하는 더 좋은 방법이 있습니까?

<LineBreak />

이것은 나를 위해 작동하지 않습니다 Text. 텍스트 문자열이 외부 소스에서 설정되기 때문에 속성 값이어야합니다 .

나는 익숙 LineBreak하지만 내가 찾는 답이 아닙니다.


나는 이것이 오래된 질문을 복원한다는 것을 알고 있지만 같은 문제가있었습니다. 나를위한 해결책은 HTML로 인코딩 된 줄 바꿈 ( &amp;#10;) 을 사용하는 것이 었습니다 .

Line1&amp;#10;Line2

처럼 보인다

Line1
Line2

HTML 인코딩 문자에 대한 자세한 내용은 w3schools를 확인하십시오.


이 시도:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>

가장 쉬운 방법은

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

따라서 XAML 코드를 작성하면는 HTML에서 <LineBreak />와 정확히 동일한 의미를 갖
거나 C #에서 "\ n"을 의미합니다 .


<LineBreak/>

http://www.longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx


줄을 두 개의 태그로 나누는 것은 어떻습니까?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>

올바른 사용 방법은 다음과 같습니다.

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>

<LineBreak />는 Grid 또는 StackPanel과 같은 컬렉션 내에있는 경우 작동하지 않습니다. 이러한 경우 다음과 같이 작동합니다.

컬렉션 내부의 LineBreak


  <HyperlinkButton 
        Content="Apply and restart this pplication!&#10;&#13;Note that modifying these settings requires the application to be restarted."   />

CRLF 간단한 방법 = !&#10;&#13;

!&#10;&#13; -TextBlock, HyperlinkText 등과 같은 모든 wpf, xaml, silverlight 컨트롤에서 작업


TextBlock의 텍스트를 바인딩하는 경우 다른 답변은 작동하지 않습니다. 분리하려는 바인딩 텍스트에 '\ n'을 추가하기 만하면됩니다.


AccessText 컨트롤을 사용하십시오. 레이블처럼 사용할 수 있으며 TextWrapping = "WrapWithOverflow"속성이 있습니다.

예.

Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.


I'm late to the party but .. this is more or less how I did it ,(mind my ItemSources are plain strings, not formatted , and I didn't need to 'convertBack' anything)

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

This also works fine:

<TextBlock>
    <Run Text="My nice text"/>
    <LineBreak/>
    <LineBreak/>
    <Run Text="After some linebreaks, I'm back!"/>
</TextBlock>

I was having a similar problem and wanted to bind a String of xaml markup to a TextBlock. Essentialy storing the declarative markup inside a TextBlock in a string for later use.

This is how I did: I subclassed the TextBlock to make the InlineCollection bindable and wrote a Converter between the string and an InlineCollection(or actually a generic list of Inlines.)

참고URL : https://stackoverflow.com/questions/837367/wpf-textblock-linebreak-in-text-attribute

반응형