Andreas Zita
Andreas Zita

Reputation: 7580

How do I make TextBlock render overflowing text when using WrapWithOverflow? (without clipping)

I'm trying to make a WPF TextBlock wrap it's text and also still render overflowing text.

I've tried settings ClipToBounds to false but it still clips the overflowing text. Any way to get around this?

<Window x:Class="overflow_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid Margin="100" ClipToBounds="False">
      <TextBlock Text="wefeiqwufhqoeirugfh qeorghuiqeo riguh qeroguihqerogiuqhergo qerhugqeuirhgeiug oqeirugh qoeruig" TextWrapping="WrapWithOverflow" ClipToBounds="False" />
    </Grid>
  </Grid>
</Window>

enter image description here

UPDATE

In my real scenario (the one provided here was just to show the problem) I have many textblocks in an itemscontrol with the same fixed width. I can not adjust the width of each item to exactly fit the text without overflow, yet I need to use WrapWithOverflow to avoid wrapping at individual characters which I would get if I was using standard Wrap. So when using WrapWithOverflow I was assuming that the overflowing text was clipped and that I could turn this off by setting ClipToBounds to false. But still the overflowing text is clipped. I find this strange, maybee it's a bug?

Another image showing what I mean:

enter image description here

with this code:

<Window x:Class="overflow_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="308">
  <Grid>
    <TextBlock Width="110" HorizontalAlignment="Center" ClipToBounds="False" VerticalAlignment="Center" Background="White" Text="wefeiqwufhqoeirugfh qeorghuiqeo riguh qeroguihqerogiuqhergo qerhugqeuirhgeiug oqeirugh qoeruig" TextWrapping="WrapWithOverflow" Margin="100,120,90,120" />
  </Grid>
</Window>

As you can see, the text is clipped on the "h" on line 1 and the text after "e" on line 3, even though I'm telling it to not clip any overflowing content. I want overflowing text to be rendered, not clipped.

Upvotes: 0

Views: 1603

Answers (3)

Andreas Zita
Andreas Zita

Reputation: 7580

So it seems it is not possible to allow overflowing text to render (not clipping) - not even by setting ClipToBound to false.

My solution is to avoid using normal text-wrapping and instead replace all " " with "\r\n" in the text and accept that I get a new line after each word. In my case this is not a problem so it works for me.

Upvotes: 1

Sheridan
Sheridan

Reputation: 69985

When I said Please explain exactly what you want, I thought you might actually describe what you wanted in a little more detail. Your code would already look like your desired output if you just removed it from the Grid with the Margin... try this:

<TextBlock Text="wefeiqwufhqoeirugfh qeorghuiqeo riguh qeroguihqerogiuqhergo 
    qerhugqeuirhgeiug oqeirugh qoeruig" TextWrapping="WrapWithOverflow" 
    HorizontalAlignment="Center" VerticalAlignment="Center" Width="160" />

The trick to wrapping text in WPF is to set the Width of the Textblock so that it knows when it should start to wrap the text:

enter image description here

Upvotes: 1

Jay
Jay

Reputation: 841

Your markup is just fine, your problem is that you shrink the window more than it has room to show the text you can play with that Margin="100" to see my point.

Upvotes: 0

Related Questions