标签和网格自动调整高度和宽度

时间:2013-05-27 07:15:56

标签: c# wpf grid label

我正在尝试创建聊天应用程序。我正在尝试在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。有什么想法和建议吗?谢谢!

1 个答案:

答案 0 :(得分:2)

您需要为文本换行指定TextWrappingLabel本身不支持TextWrapping。您有两个选项可将Label切换为TextBlock或将TextBlock嵌入Label

类似的东西:

<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,因为它比使用StackPanelDockPanelWrapPanel和排序更昂贵。如果不是,我们就不会有任何其他布局控制,而是网格。

其次,WPF中的Label与OSX Cocoa或Qt中的Label或者其他语言不同。它比WPF中的TextBlock更昂贵。阅读this以了解差异并自行评估是否确实需要在此使用Label

同时获取Snoop并查看其快速帮助视频以查看其使用情况。它将成为您诊断布局问题的首选助手。

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

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