wpf TextBlock TextWrapping 不换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6625177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:57:47  来源:igfitidea点击:

TextBlock TextWrapping not wrapping

wpfxaml

提问by Shaun Bowe

When I place a TextBlock inside of a Horizontally Aligned StackPanel it does not wrap. I realize that this is because the available width of the StackPanel is PositiveInfinity but are there any workarounds?

当我将 TextBlock 放置在水平对齐的 StackPanel 内时,它不会换行。我意识到这是因为 StackPanel 的可用宽度是 PositiveInfinity 但有什么解决方法吗?

My layout is much more complicated than this sample so I cannot remove the StackPanel or the Horizontal Orientation. I just tried to reproduce the simplest possible example that exhibits the behavior.

我的布局比此示例复杂得多,因此我无法删除 StackPanel 或 Horizo​​ntal Orientation。我只是试图重现展示该行为的最简单的示例。

    <StackPanel Orientation="Horizontal">
        <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />
        <Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" />
        <TextBlock TextWrapping="Wrap"
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
    </StackPanel>

Update:The width of the TextBlock must be dynamic. I need it to flow with the window as it is resized.

更新:TextBlock 的宽度必须是动态的。我需要它在调整大小时与窗口一起流动。

Update 2:Added another element to the StackPanel because I need the children laid out Horizontally.

更新 2:向 StackPanel 添加了另一个元素,因为我需要水平布置子项。

Update 3 (Solution):Replaced the StackPanel with a DockPanel.

更新 3(解决方案):将 StackPanel 替换为 DockPanel。

<DockPanel>
    <DockPanel DockPanel.Dock="Top">
        <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" DockPanel.Dock="Left" />
        <Rectangle Width="50" Height="50" Fill="Red" VerticalAlignment="Top" DockPanel.Dock="Left" />
        <TextBlock TextWrapping="Wrap"
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
    </DockPanel>
</DockPanel>

回答by Dan Puzey

It's because you're using Horizontal orientation on the StackPanel. That means that the StackPanel is giving full widthto each child control, and then laying them out horizontally - even if that means exceeding its bounded/visible width. Because there's nothing to constrain the width of the TextBlock, it doesn't wrap.

这是因为您在 StackPanel 上使用了水平方向。这意味着 StackPanel 为每个子控件提供全,然后将它们水平放置 - 即使这意味着超出其有界/可见宽度。因为没有什么可以限制 TextBlock 的宽度,所以它不会换行。

If you switch to Vertical orientation then the wrapping works, but I'm guessing there's a reason for you specifying otherwise. Can you show what layout you're trying to achieve?

如果您切换到垂直方向,则包装会起作用,但我猜您有理由另外指定。你能展示你想要实现的布局吗?

回答by Leom Burke

You could use a grid instead of the StackPanel (which as explained does not restrict its content.). A grid allows much more control over layout of items than a StackPanel does and if your image is collapsed then the 'auto' column will have a 0 width.

您可以使用网格而不是 StackPanel(正如解释的那样不限制其内容。)。与 StackPanel 相比,网格允许对项目布局进行更多控制,如果您的图像折叠,则“自动”列的宽度将为 0。

<DockPanel>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Width="50" Height="50" Fill="Blue" VerticalAlignment="Top" />
        <TextBlock TextWrapping="Wrap" Grid.Column="1"
            Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
    </Grid>

</DockPanel>

回答by Bathineni

I got couple of solution for this problem.

对于这个问题,我有几个解决方案。

1) binding parent width to textblock width. (im the following case i considered usercontrol as parent).

1) 将父宽度绑定到文本块宽度。(我在以下情况下将 usercontrol 视为父级)。

<UserControl x:Class="WpfApplication1.MyWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="userctrl">   
    <DockPanel>
        <StackPanel Orientation="Horizontal" DockPanel.Dock="Top" HorizontalAlignment="Left" >
            <TextBlock TextWrapping="Wrap" MaxWidth="{Binding ElementName=userctrl,Path=ActualWidth}"
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />

        </StackPanel>       
    </DockPanel>
</UserControl>

2) other solution is using Grid instead of stackpanel

2) 其他解决方案是使用 Grid 而不是 stackpanel

<UserControl x:Class="WpfApplication1.MyWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="userctrl">   
    <DockPanel>
        <Grid DockPanel.Dock="Top" HorizontalAlignment="Left" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock TextWrapping="Wrap" 
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />
            <TextBlock TextWrapping="Wrap" Grid.Column="1" Margin="20 0 0 0"
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo lectus, viverra ut lobortis vel, mollis eget lectus. Suspendisse laoreet consequat ultrices. Curabitur ultricies, tortor feugiat porttitor faucibus, lorem eros pretium nisl, eu ullamcorper mauris tortor sit amet augue." />

        </Grid>       
    </DockPanel>
</UserControl>

回答by Connor Mcgrann

If you were like me and was wondering why the Textblock was still not wrapping after defining a Width.

如果您像我一样想知道为什么在定义宽度后 Textblock 仍然没有换行。

Try adding a MaxWidth to the Textblock with same same value as Width,

尝试将 MaxWidth 添加到具有与 Width 相同值的 Textblock,

This stops the control from growing in Width once the application has started.

一旦应用程序启动,这将阻止控件的宽度增长。

Hope this helps!

希望这可以帮助!