TextBlock TextWrapping 不换行 [英] TextBlock TextWrapping not wrapping

查看:42
本文介绍了TextBlock TextWrapping 不换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的布局比此示例复杂得多,因此我无法删除 StackPanel 或水平方向.我只是试图重现展示该行为的最简单的示例.

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 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>

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

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

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

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

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

Update 3 (Solution): Replaced the StackPanel with a 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>

推荐答案

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

It's because you're using Horizontal orientation on the StackPanel. That means that the StackPanel is giving full width to 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.

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

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?

这篇关于TextBlock TextWrapping 不换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆