当文本被设置为属性时,我如何添加换行符:

<TextBlock Text="Stuff on line1 \n Stuff on line2" />

把它分解成分解格式不适合我的特殊情况。我需要的是一些方法来模仿以下:

<TextBlock>
  <TextBlock.Text>
    Stuff on line1 <LineBreak/>
    Stuff on line2
  </TextBlock.Text>
<TextBlock/>

当前回答

<TextBox 
    Name="myTextBox" 
    TextWrapping="Wrap" 
    AcceptsReturn="True" 
    VerticalScrollBarVisibility="Visible" />

其他回答

也许你可以使用属性xml:space="preserve"来保存源XAML中的空白

<TextBlock xml:space="preserve">
Stuff on line 1
Stuff on line 2
</TextBlock>

也不工作

<TextBlock><TextBlock.Text>NO USING ABOVE TECHNIQUE HERE</TextBlock.Text>

没什么大不了的,只是需要用一下

<TextBlock Text="Cool &#x0a;Newline trick" />

代替。

也许有人更喜欢

<TextBlock Text="{Binding StringFormat='Stuff on line1{0}Stuff on line2{0}Stuff on line3',
                          Source={x:Static s:Environment.NewLine}}" />

xmlns: s =“clr-namespace:系统;装配= mscorlib”。

你只需要删除<TextBlock。文本>,并简单地添加您的内容如下:

    <Grid Margin="20">
        <TextBlock TextWrapping="Wrap" TextAlignment="Justify" FontSize="17">
        <Bold FontFamily="Segoe UI Light" FontSize="70">I.R. Iran</Bold><LineBreak/>
        <Span FontSize="35">I</Span>ran or Persia, officially the <Italic>Islamic Republic of Iran</Italic>, 
        is a country in Western Asia. The country is bordered on the 
        north by Armenia, Azerbaijan and Turkmenistan, with Kazakhstan and Russia 
        to the north across the Caspian Sea.<LineBreak/>
        <Span FontSize="10">For more information about Iran see <Hyperlink NavigateUri="http://en.WikiPedia.org/wiki/Iran">WikiPedia</Hyperlink></Span>
            <LineBreak/>
            <LineBreak/>
            <Span FontSize="12">
                <Span>Is this page helpful?</Span>
                <Button Content="No"/>
                <Button Content="Yes"/>
            </Span>
    </TextBlock>
    </Grid>

注意,要做到这一点,你需要在文本属性中这样做,你不能使用像这样的内容

<TextBlock>Stuff on line1&#x0a;Stuff on line 2</TextBlock>