繁体   English   中英

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

[英]Label and grid auto height and width adjust

我正在尝试创建一个聊天应用程序。 我正在尝试在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 你有两个选择切换LabelTextBlock或嵌入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和排序更昂贵。 如果不是,我们将不会有任何其他布局控制,但网格。

其次, Label在WPF是不喜欢的Label在OSX可可或Qt的也许其他语言了。 它比WPF中的TextBlock更昂贵。 通读了解这些差异,并评估自己,如果你确实需要使用Label在这里。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM