0

我正在尝试创建一个聊天应用程序。Label我正在尝试在一个包裹内显示一系列文本Grid

<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。有什么想法和建议吗?谢谢!

4

1 回答 1

2

您需要指定TextWrapping文本换行。Label本身不支持TextWrapping。您有两个选项将 a 切换Label到 aTextBlock或将 a 嵌入TextBlockLabel.

就像是:

<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随处使用所有东西,因为使用它比 say StackPanelDockPanelWrapPanelsorts 更昂贵。如果不是这样,除了 Grid 之外,我们将没有任何其他布局控件。

其次,LabelWPF 不像LabelOSX Cocoa 或 Qt 或者其他语言。它比TextBlockWPF 中的 a 贵。通读本文以了解差异并自行评估是否真的需要在Label这里使用。

还可以获取Snoop并通过它的快速帮助视频查看它的用法。它将成为您诊断布局问题的首选助手。

哦,还请查看一些现有的使用 WPF 的开源聊天示例以指导您。您可以忽略有关后端服务的所有内容,只关注 xaml 位,看看作者为应用程序的哪个部分选择了哪些控件,并尝试理解其中的原因。我这样说是因为您添加Label每条消息的方法很快就会涉及从代码隐藏或奇怪的东西创建控件,这有点令人不悦。使用一个ListView或另一个自定义控件可能是您最终要重构您的应用程序的内容。如果您访问一些现有的示例并自学,只会为您省去所有麻烦。

最后欢迎来到WPF!!!这是一个很棒的地方:)

于 2013-05-27T09:04:43.877 回答