WPF布局:自动换行不起作用

时间:2009-01-26 09:57:54

标签: wpf layout textblock

为什么我的TextBlock中的文本会延伸到画布之外的右边,即使我指定了自动换行?

<Window x:Class="WpfApplication6.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" SizeToContent="WidthAndHeight">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="111"/>
            <Button Content="222"/>
            <Button Content="333"/>
            <Button Content="444"/>
            <Button Content="555"/>
        </StackPanel>
        <StackPanel DockPanel.Dock="Left">
            <Button Content="One"/>
            <Button Content="Two"/>
            <Button Content="Three"/>
            <Button Content="Four"/>
            <Button Content="Five"/>
        </StackPanel>
        <Canvas Background="tan">
            <TextBlock TextWrapping="Wrap">This is the content in this area here</TextBlock>
        </Canvas>
    </DockPanel>
</Window>

解决方案:谢谢,史蒂夫,这样做了,我添加了一个ScrollViewer,很好:

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <Button Content="111"/>
        <Button Content="222"/>
        <Button Content="333"/>
        <Button Content="444"/>
        <Button Content="555"/>
    </StackPanel>
    <StackPanel DockPanel.Dock="Left">
        <Button Content="One" Click="Button_Click" />
        <Button Content="Two"/>
        <Button Content="Three"/>
        <Button Content="Four"/>
        <Button Content="Five"/>
    </StackPanel>
    <Grid Background="tan">
        <ScrollViewer>
            <TextBlock Name="mainArea" Padding="10" TextWrapping="Wrap">This is the content in this area here</TextBlock>
        </ScrollViewer>
    </Grid>
</DockPanel>

1 个答案:

答案 0 :(得分:3)

它在Canvas中,因此没有设置任何宽度。如果您不需要画布,请将其更改为Grid(自动调整大小),TextBlock将正确换行。