2

I can't seem to ever get HasOverflowContent to fire on a RichTextBlock, even if I force it to update its layout. Here is what I've tried:

public MainPage()
{
    this.InitializeComponent();
    RichTextBlock block = new RichTextBlock();
    RichTextBlockOverflow overflow = new RichTextBlockOverflow();
    block.OverflowContentTarget = overflow;
    block.Width = 100;
    block.Height = 100;
    Paragraph paragraph = new Paragraph();
    for (int i = 0; i < 1000; i++)
    {
        Run run = new Run();
        run.Text = "FILLER";
        paragraph.Inlines.Add(run);
    }
    block.Blocks.Add(paragraph);
    block.UpdateLayout();
    if (block.HasOverflowContent)
    {
        // This line will never be hit.
    }
    MainGrid.Children.Add(block);
}

Why isn't HasOverflowContent signaling it that it does, in fact, have overflow content?

Edit: Okay, I was able to subscribe to this.Loaded on the MainPage constructor and this setting does show up correctly in such a case, however, its not good enough for my application so how can I check this outside of the loaded event?

Alexandru
  • 12,264
  • 17
  • 113
  • 208
  • 1
    +1 the remarks [here](http://msdn.microsoft.com/en-US/library/windows/apps/windows.ui.xaml.uielement.updatelayout) seem to indicate `UpdateLayout` can be used precisely for this purpose. – Lukazoid Dec 15 '13 at 21:44
  • @Lukazoid That's precisely what I'm trying to do above, but it seems this will only work under certain circumstances. Circumstances in which I just don't understand! – Alexandru Dec 15 '13 at 22:23
  • 1
    Have you tried forcing the `CoreDispatcher` to process pending messages before checking the flag, i.e. `block.CoreDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {}).Wait()`? – Lukazoid Dec 15 '13 at 22:25
  • @Lukazoid Yeah, this seems to have the same effect :/ I edited the question to add this in and highlight this point as well – Alexandru Dec 16 '13 at 00:04
  • 1
    Your implementation does not wait for `RunBlockDispatcher` to complete before checking `HasOverflowContent` so will not work. I am going to write up some dummy code and see if I can solve this. – Lukazoid Dec 16 '13 at 00:12
  • @Lukazoid Shoot, sorry Luke, give me a few minutes to work this out. I'm new to async programming. – Alexandru Dec 16 '13 at 00:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43226/discussion-between-lukazoid-and-alexandru) – Lukazoid Dec 16 '13 at 00:17
  • @Lukazoid So, it looks like it has to be added to the Visual Tree. It won't render it otherwise. Once added, I can call RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); and await renderTargetBitmap.RenderAsync(contentToBePrinted); I can make this work for printing from metro apps by adding the RichTextBlock in a Grid with 0 opacity, and all RichTextBlockOverflow's also to that grid on run-time, so that I can get them to render and also check the HasOverflowContent flag. Its the only way I could make it work the way I want it to. – Alexandru Dec 16 '13 at 18:57

1 Answers1

0

So, it looks like it has to be added to the Visual Tree. It won't render it otherwise. Once added, I can call RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); and await renderTargetBitmap.RenderAsync(contentToBePrinted); I can make this work for printing from metro apps by adding the RichTextBlock in a Grid with 0 opacity, and all RichTextBlockOverflow's also to that grid on run-time, so that I can get them to render and also check the HasOverflowContent flag. Its the only way I could make it work the way I want it to.

Alexandru
  • 12,264
  • 17
  • 113
  • 208