[英]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.