[英]Label and grid auto height and width adjust
I'm trying to create a chat application. 我正在尝试创建一个聊天应用程序。 I'm trying to display a series of text in a Label
wrapped inside a Grid
. 我正在尝试在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>
As you can see I set a max width hoping that when the text in the label reached this width, the grid will adjust its height as to process all the text inside the label. 如您所见,我设置了最大宽度,希望当标签中的文本达到此宽度时,网格将调整其高度以处理标签内的所有文本。 But it won't work. 但它不会起作用。 This is my first time to try WPF
. 这是我第一次尝试WPF
。 Any ideas and suggestions? 有什么想法和建议吗? Thanks! 谢谢!
You need to specify TextWrapping
for text wrap. 您需要为文本换行指定TextWrapping
。 Label
does not support TextWrapping
by itself. Label
本身不支持TextWrapping
。 You got two options switch the Label
to a TextBlock
or embed a TextBlock
inside the Label
. 你有两个选择切换Label
到TextBlock
或嵌入TextBlock
内部Label
。
something like: 就像是:
<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>
or 要么
<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>
sidenotes : 旁注 :
Since it's your first time to try WPF, I'd suggest reading up a bit more from books or source codes about layout guidelines. 由于这是您第一次尝试使用WPF,我建议您阅读有关布局指南的书籍或源代码。
Firstly Grid
can be made to do almost anything another layout control can do. 首先, Grid
可以做几乎任何其他布局控制可以做的事情。 This does not mean you just use Grid
everywhere for everything since it's more expensive to use than say StackPanel
, DockPanel
, WrapPanel
and sorts. 这并不意味着你只需要在任何地方使用Grid
,因为使用它比使用StackPanel
, DockPanel
, WrapPanel
和排序更昂贵。 If it wasn't we wouldn't have any other layout control but the Grid. 如果不是,我们将不会有任何其他布局控制,但网格。
secondly, Label
in WPF isn't like the Label
in OSX Cocoa or Qt or maybe other languages too. 其次, Label
在WPF是不喜欢的Label
在OSX可可或Qt的也许其他语言了。 It's more expensive than a TextBlock
in WPF. 它比WPF中的TextBlock
更昂贵。 Read through this to understand the differences and assess for yourself if you really need to be using a Label
here. 通读此了解这些差异,并评估自己,如果你确实需要使用Label
在这里。
Also get Snoop and go through it's quick help videos to see it's usage. 还可以获取Snoop并通过它的快速帮助视频来查看它的用法。 It will become your go-to helper for diagnosing Layout problems. 它将成为诊断布局问题的首选助手。
Oh and also look at some existing open-source chat examples using WPF like this to guide you. 哦,也要看使用WPF像一些现有的开放源代码聊天例子这个来指导你。 You could ignore all the bit's about back-end services and focus just on the xaml bits and see what control's the author picks for which section of the application and try to understand the reasoning. 你可以忽略关于后端服务的所有内容,只关注xaml位,看看作者选择了哪个控件用于应用程序的哪个部分并尝试理解推理。 I say this because your approach of adding a Label
per message is soon going to either involve creating control's from the code-behind or weirder stuff which is kinda frowned upon. 我这样说是因为你为每条消息添加一个Label
方法很快就会涉及从代码隐藏或者更奇怪的东西创建控件,这有点令人不悦。 Using a ListView
or another custom control might be what you're eventually going to refactor your app into. 使用ListView
或其他自定义控件可能是您最终要将您的应用重构为的内容。 Just saves you all that hassle if you visit some existing samples and teach yourself. 如果你访问一些现有的样本并教自己,只需省去所有麻烦。
Finally welcome to WPF!!! 最后欢迎来到WPF! it's a great place here :) 这是一个很棒的地方:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.