Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBlock Padding is cutting off text

I have a TextBlock in a Grid with its Padding attribute set to 5. Sometimes the last character is cut off, depending on what string the Text property is set to.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SomeClass">
    <ScrollViewer Padding="5" VerticalScrollBarVisibility="Auto">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Label
                Grid.Row="0" Grid.Column="0"
                Content="SomeLabel"
                HorizontalAlignment="Right"
                HorizontalContentAlignment="Right"
                VerticalAlignment="Center" />
            <TextBlock
                Grid.Row="0" Grid.Column="1"
                HorizontalAlignment="Left"
                Padding="5"
                Text="0x0F"
                TextWrapping="Wrap"
                VerticalAlignment="Top" />
        </Grid>
    </ScrollViewer>
</UserControl>

When the Text is set to 0x0F the F is not visible. When it is set to 0xAB the string displays just fine. Setting the Padding to 0 also makes the string display just fine.

like image 242
sourcenouveau Avatar asked Sep 14 '09 18:09

sourcenouveau


1 Answers

What you describe is obviously a layout bug in WPF (probably in the TextBlock). Whether or not the last letter is wrapped (and cut off) seems to depends on the actual width of the string and the size of the last letter in respect to the size of the padding.

I suggest you report the bug here.

To work around this issue you can use the following code (just put a border around you textblock and set the padding there instead):

<Border Padding="5" Grid.Row="0" Grid.Column="1">
    <TextBlock  HorizontalAlignment="Left"
        Text="0x0F" TextWrapping="Wrap"
        VerticalAlignment="Top" />
</Border>
like image 53
bitbonk Avatar answered Oct 19 '22 18:10

bitbonk