XAML TextBlock:如何使TextBlock具有可变高度?
我有一个包含 TextBlock 的列表框。
有时 TextBlock 的内容太长,我希望该条目的高度根据需要增加一倍或三倍以容纳文本。
我尝试了 TextWrapping="Wrap" 但它不起作用。每个 TextBlock 的高度仍然只有一行。
有没有一种简单的方法可以解决 XAML 中的问题?谢谢。
*附加信息:我试图简化问题,但也许完整的场景更好。
- 我有一个列表框,其条目根据下面代码中的模板显示。
- 每个条目都有 2 条信息:产品价格和产品名称。
- 我不想在列表框中使用水平滚动条,并且希望产品名称在必要时显示在 2 行或更多行中。产品名称为 2nd TextBlock。
这是我的 XAML:
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock MinWidth="40" TextAlignment="Right" Text = "{Binding ProductPrice}" />
<TextBlock Text = "{Binding ProductName}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a ListBox that contains TextBlocks.
Sometimes the content of a TextBlock is too long and I want the height of this entry to double or triple as needed to accommodate the text.
I tried TextWrapping="Wrap" but it doesn't work. Every TextBlock is still just one line in height.
Is there an easy way to fix the problem in XAML? Thanks.
* Additional info: I tried to simplify the question but perhaps the complete scenario is better.
- I have a listbox whose entries are displayed according to a template in code below.
- Each entry has 2 pieces of info: a product price followed by product name.
- I don't want to use the horizontal scrollbar in the listbox and want the product name to be displayed in 2 or more lines if necessary. The product name is the 2nd TextBlock.
Here's my XAML:
<ListBox Name="listBox1" ItemsSource="{Binding}" Margin="10" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock MinWidth="40" TextAlignment="Right" Text = "{Binding ProductPrice}" />
<TextBlock Text = "{Binding ProductName}" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
禁用列表框水平
scrollViewer
。这样textBlock
将被强制换行。XAML:
示例结果:
编辑:
从您添加的 XAML 中,我是确信问题出在
StackPanel
上。尝试用
Grid
替换它,例如:StackPanel
不限制内容大小,因此textBlock
不知道空间在哪里结束,并且包装不会发生。Disable the listbox horizontal
scrollViewer
. This way thetextBlock
will be forced to wrap.XAML:
Example result:
Edit:
From the XAML you added I'm confident that the problem lays in the
StackPanel
.Try replacing it with
Grid
for example:StackPanel
doesn't limit the content size, therefore thetextBlock
doesn't knows where the space ends , and the wrapping doesn't happens.您正在使用
StackPanel
。尝试使用DockPanel
:例如:
You are using
StackPanel
. Try usingDockPanel
:For Example:
这会帮助你做到这一点。不要调整
TextBlock
的大小,只需调整滚动查看器的大小,因为 texblock 需要可变,因此ScrollViewer
一旦超过的大小,就会应用
。Scrollbar
>ScrollViewer对于列表框项目
this will help you do that. Dont Size the
TextBlock
just size the scroll viewer because texblock needs to be variable soScrollViewer
will applyScrollbar
once it goes beyond the size ofScrollViewer
.For ListBoxItem