如何在 WPF WrapPanel 中将几个标签居中对齐?

问题描述 投票:0回答:1

我在 WPF StackPanel 和 WrapPanel 中放置了一些标签,如下所示,这样我就可以用粗体显示其中的部分内容。

    <Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2">
        <StackPanel  Margin="10,15,20,5" >
            <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <Label Name="WarningLabelTextLookOverPart1"  
                   Content="{Binding WarningLabelTextLookOverPart1}"
                   FontFamily="{DynamicResource NormalFontFamily}"
                   FontSize="{DynamicResource NormalFontSize}"
                   FontWeight="Normal"
                   Style="{DynamicResource H2Label}" 
                   Foreground="{DynamicResource BlackEightyBrush}"
                   Visibility="{Binding ShowIdCapture, Converter={StaticResource BooleanVisibilityConverterCollapsedWhenFalse}}"/>
                <Label Name="WarningLabelTextLookOverPart2"  
                   Content="{Binding WarningLabelTextLookOverPart2}"
                   FontFamily="{DynamicResource NormalFontFamily}" 
                   FontSize="{DynamicResource NormalFontSize}"
                   FontWeight="Bold"
                   Style="{DynamicResource H2Label}" 
                   Foreground="{DynamicResource BlackEightyBrush}"
                   Visibility="{Binding ShowIdCapture, Converter={StaticResource BooleanVisibilityConverterCollapsedWhenFalse}}"/>
                <Label Name="WarningLabelTextLookOverPart3"  
                   Content="{Binding WarningLabelTextLookOverPart3}"
                   FontFamily="{DynamicResource NormalFontFamily}"
                   FontSize="{DynamicResource NormalFontSize}"
                   FontWeight="Normal"
                   Style="{DynamicResource H2Label}" 
                   Foreground="{DynamicResource BlackEightyBrush}"
                   Visibility="{Binding ShowIdCapture, Converter={StaticResource BooleanVisibilityConverterCollapsedWhenFalse}}"/>
                <Label Name="WarningLabelTextLookOverPart4"  
                   Content="{Binding WarningLabelTextLookOverPart4}"
                   FontFamily="{DynamicResource NormalFontFamily}"
                   FontSize="{DynamicResource NormalFontSize}"
                   FontWeight="Bold"
                   Style="{DynamicResource H2Label}" 
                   Foreground="{DynamicResource BlackEightyBrush}"
                   Visibility="{Binding ShowIdCapture, Converter={StaticResource BooleanVisibilityConverterCollapsedWhenFalse}}"/>
            </WrapPanel>
        </StackPanel>
    </Grid>

看起来像这样

有关如何将标签集中对齐的任何建议,即将“您被起诉”部分居中。

wpf wpf-controls
1个回答
0
投票

您可以将

UniformGrid
与 1 列与
WrapPanel
组合使用,如下所示

<Grid Grid.Row="1"
      Grid.RowSpan="2"
      Grid.Column="0"
      Grid.ColumnSpan="2">
    <StackPanel Margin="10,15,20,5">
        <UniformGrid HorizontalAlignment="Center" Columns="1">
            <WrapPanel Orientation="Horizontal">
                <Label Name="WarningLabelTextLookOverPart1"
                       Content="If the customer look under 25,"
                       FontWeight="Normal"
                       Foreground="Black"/>
                <Label Name="WarningLabelTextLookOverPart2"
                       Content="it's YOUR reposibility to ask for ID"
                       FontWeight="Bold"
                       Foreground="Black"/>
                <Label Name="WarningLabelTextLookOverPart3"
                       Content="to check this and failure to do so could result in"
                       FontWeight="Normal"
                       Foreground="Black"/>
            </WrapPanel>

            <Label Name="WarningLabelTextLookOverPart4"
                   HorizontalAlignment="Center"
                   Content="you being prosecuted"
                   FontWeight="Bold"
                   Foreground="Black"/>
        </UniformGrid>
    </StackPanel>
</Grid>

下次提问时,请提供最小的可重现示例,即将绑定更改为实际文本。

© www.soinside.com 2019 - 2024. All rights reserved.