【问题标题】:Label and grid auto height and width adjust标签和网格自动调整高度和宽度
【发布时间】:2013-05-27 07:15:55
【问题描述】:

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

【问题讨论】:

    标签: c# wpf grid label


    【解决方案1】:

    您需要指定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,因为使用它比使用StackPanelDockPanelWrapPanel 和排序更昂贵。如果不是这样,除了 Grid 之外,我们将没有任何其他布局控件。

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

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

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

    终于欢迎来到 WPF !!!这里是个好地方:)

    【讨论】:

      猜你喜欢
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2015-03-27
      • 1970-01-01
      • 2023-03-15
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多