简体   繁体   English

WPF 标签中的文本在行尾被截断

[英]Text in WPF label is being cut-off at the end of the line

I have an WPF usercontrol which has a grid with 4 columns and only 1 row.我有一个 WPF 用户控件,它有一个 4 列和只有 1 行的网格。

The second column is a WPF label.第二列是 WPF 标签。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>

    // I only specify there the label, no other components, just to simplify
    <Label Grid.Column="1"
           VerticalAlignment="Center"
           Margin="5"
           Content="{Binding Path=Text}"
           Foreground="{Binding Path=ForegroundColor}">
        <Label.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="TextWrapping" Value="Wrap" />
            </Style>
        </Label.Resources>
    </Label>
</Grid>

For some reason the text of the label is being cut-off at the end of the line instead of putting it at next line.由于某种原因,标签的文本在行尾被截断,而不是放在下一行。 I have tried above solution to wrap text and continue in the next line but it is not working.我已经尝试了上述解决方案来包装文本并在下一行继续,但它不起作用。 Any ideas?有任何想法吗?

UPDATE : I changed the Label as below according to this :更新:我根据以下更改了标签:

    <Label Grid.Column="1"
           VerticalAlignment="Center"
           VerticalContentAlignment="Center"
           HorizontalContentAlignment="Stretch"
           HorizontalAlignment="Stretch"
           Height="Auto"
           Margin="5"
           Foreground="{Binding Path=ForegroundColor}">
        <TextBlock Text="{Binding Path=Text}" TextWrapping="Wrap"/>
    </Label>

and now it is working like a charm!现在它就像一个魅力!

You may use the AccessText and set the TextWrapping="Wrap" :您可以使用AccessText并设置TextWrapping="Wrap"

    <Label Grid.Column="1"
           VerticalAlignment="Center"
           VerticalContentAlignment="Center"
           HorizontalContentAlignment="Stretch"
           HorizontalAlignment="Stretch"
           Height="Auto"
           Margin="5"
           Foreground="{Binding Path=ForegroundColor}">
        <Label.Content>
            <AccessText TextWrapping="Wrap" Text="{Binding Path=Text}"/>
        </Label.Content>
    </Label>

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

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