WPF TextBlock Padding is cutting off text
Asked Answered
T

4

7

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.

Tangerine answered 14/9, 2009 at 18:53 Comment(2)
We need to see all the XAML so we can see what's wrong with your layout logic.Gondi
I've provided a complete example.Tangerine
C
7

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>
Cidevant answered 16/9, 2009 at 11:5 Comment(2)
Thank you, I will report it as you suggested.Tangerine
Reported at connect.microsoft.com/VisualStudio/feedback/…Tangerine
H
0

Make the column of the grid that contains textblock auto size like this

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBlock Grid.COlumn="0" Text="{Binding Path=SomeViewModelProperty}" />
</Grid>
Hilltop answered 14/9, 2009 at 20:39 Comment(1)
Wow, I tried your code and get the same result, it cuts off F. This also happens for 0x0b and 0x0d but other hex numbers are displayed properly.Hilltop
B
0

If you set the height on the TextBlock to 100, does the F then get wrapped?

Baron answered 14/9, 2009 at 20:40 Comment(1)
This was a good guess, but no, unfortunately I tried what you suggested and the F was not wrapped.Tangerine
G
0

Just increase the Height of the ComboBoxItem , it should solve the problem.

Gamelan answered 8/11, 2016 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.