标签和网格的高度和宽度自动调整
本文关键字:调整 高度 网格 标签 | 更新日期: 2023-09-27 18:15:23
我正在尝试创建一个聊天应用程序。我试图在Grid
内包装的Label
中显示一系列文本。
<Grid Grid.Row="2">
<Grid Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Width="auto" MaxWidth ="300" Height="auto" HorizontalAlignment="Left" Margin="10,5,0,0" ScrollViewer.CanContentScroll="True">
<Grid.Background>
<ImageBrush ImageSource="Public'Images'chat_green-textarea.png"/>
</Grid.Background>
<Label Padding="5,0,5,5" Foreground="White" FontSize="15">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</Label>
</Grid>
</Grid>
<Image Source="Public'Images'chat_green-textarea-tail.png" Height="20" Width="30" VerticalAlignment="Top" Margin="-20,29.5,0,0"/>
</Grid>
</Grid>
正如你所看到的,我设置了一个最大宽度,希望当标签中的文本达到这个宽度时,网格会调整它的高度,以处理标签内的所有文本。但这行不通。这是我第一次尝试WPF
。有什么想法和建议吗?谢谢!
您需要指定TextWrapping
用于文本换行。Label
本身不支持TextWrapping
。您有两种选择,将Label
切换到TextBlock
或在Label
中嵌入TextBlock
。
类似:
<TextBlock FontSize="15"
Foreground="White"
Padding="5,0,5,5"
TextWrapping="Wrap">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</TextBlock>
或
<Label FontSize="15"
Foreground="White"
Padding="5,0,5,5">
<TextBlock TextWrapping="Wrap">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</TextBlock>
</Label>
旁注:
因为这是你第一次尝试WPF,我建议你多阅读一些关于布局指南的书籍或源代码。
首先,Grid
可以做几乎任何其他布局控件可以做的事情。这并不意味着你在任何地方都可以使用Grid
,因为它比使用StackPanel
、DockPanel
、WrapPanel
和排序更昂贵。如果不是,我们就不会有任何其他布局控件,除了网格。
其次,WPF中的Label
不像OSX Cocoa或Qt或其他语言中的Label
。它比WPF中的TextBlock
更贵。通读这篇文章,了解它们之间的区别,并自己评估是否真的需要在这里使用Label
。
也可以使用Snoop,浏览它的快速帮助视频,看看它的用法。它将成为你诊断布局问题的助手。
哦,还要看看一些使用WPF的现有开源聊天示例来指导您。您可以忽略所有关于后端服务的部分,只关注xaml部分,看看作者为应用程序的哪个部分选择了什么控件,并尝试理解其原因。我之所以这么说,是因为你为每条消息添加Label
的方法很快就会涉及到从代码背后创建控件或更奇怪的东西,这有点令人皱眉。使用ListView
或其他自定义控件可能是你最终要重构应用程序的方式。如果你参观一些现有的样本并自学,可以省去所有的麻烦。
欢迎来到WPF!!这里是个好地方