简体   繁体   English

为什么在ItemsControl模板中的TextBlock周围需要边框才能使TextBlock换行?

[英]Why is a Border needed around a TextBlock in an ItemsControl template to make the TextBlock wrap?

I found this question while trying to figure out how to make a TextBlock wrap, when that TextBlock is the template for each item in an ItemsControl. 当TextBlock是ItemsControl中每个项目的模板时,我在尝试弄清楚如何制作TextBlock包装时发现了这个问题

My original template: 我原来的模板:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock Padding="2" x:Name="SummaryRow" Text="{Binding}" TextWrapping="Wrap" />
        </Grid>
    </DataTemplate>
</ItemsControl.ItemTemplate>

In order to make the text actually wrap, I had to surround the TextBlock with a Border. 为了使文本实际换行,我不得不用边框包围TextBlock。 I'm sure other containers would have worked as well. 我相信其他容器也会起作用。

Why is this? 为什么是这样? (btw I should mention that the ItemsControl is in a ScrollViewer) (顺便说一句我应该提到ItemsControl在ScrollViewer中)

For the text to be wrapped you need to restrict the size of the textBlock so as to wrap the text once it exceeds that restricted limit. 对于要包装的文本,您需要restrict the size of the textBlock以便在文本超出限制限制时包装文本。 But since your textBlock have scrollViewer outside, it has no restiction on its size and hence no wrap. 但是由于你的textBlock外面有scrollViewer,它的大小没有任何重复,因此没有换行。 You need to set the HorizontalScrollBarVisbility to Collapsed or Hidden to restrict the size and hence wrapping of text. 您需要将HorizontalScrollBarVisbility to Collapsed or Hidden设置HorizontalScrollBarVisbility to Collapsed or Hidden来限制文本的大小和包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM