views:

809

answers:

3

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.

A: 

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>
Wallstreet Programmer
I tried your suggestion but the F is still cut off.
emddudley
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.
Wallstreet Programmer
A: 

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

Mike Blandford
This was a good guess, but no, unfortunately I tried what you suggested and the F was not wrapped.
emddudley
Setting TextWrapping="NoWrap" corrects the issue however.
emddudley
+1  A: 

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>
bitbonk
Thank you, I will report it as you suggested.
emddudley
Reported at https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=497453
emddudley