Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when WPF textbox has finished rendering after pasting text

Tags:

c#

wpf

I have a small program written to manipulate textual data then write it to a text file. A typical workflow is this:

  1. Open an Excel file, select and copy the whole worksheet.
  2. Paste the data into a multi-line textbox in my WPF app.
  3. Choose options, click export, and let it do its thing.

Here's the problem. When pasting a lot of data (600,000 lines etc), it takes a little while till the data actually appears in the textbox even though the paste event happens immediately. I'm assuming that the delay is a rendering issue.

When the paste event fires I pop up a status window alerting the user that pasting has begun.

I want to know when the paste/redraw has finished so I can close the status window but have been unable to find any info on the subject.

I'm probably googling the wrong stuff since all I can find is work arounds for forcing a windows to redraw before continuing code execution. This seems like an ugly hack to me. I'd rather simply listen for a "render completed" event. Maybe there isn't anything like that.

like image 596
StillLearnin Avatar asked May 28 '15 19:05

StillLearnin


1 Answers

Have a look here: http://geekswithblogs.net/ilich/archive/2012/10/16/running-code-when-windows-rendering-is-completed.aspx

WPF is full of surprises. It makes complicated tasks easier, but at the same time overcomplicates easy task as well. A good example of such overcomplicated things is how to run code when you’re sure that window rendering is completed. Window Loaded event does not always work, because controls might be still rendered. I had this issue working with Infragistics XamDockManager. It continued rendering widgets even when the Window Loaded event had been raised. Unfortunately there is not any “official” solution for this problem.

But there is a trick. You can execute your code asynchronously using Dispatcher class.

Dispatcher.BeginInvoke(new Action(() => Trace.WriteLine("DONE!",
"Rendering")), DispatcherPriority.ContextIdle, null);

This code should be added to your Window Loaded event handler. It is executed when all controls inside your window are rendered. Seems to address your issue

like image 62
Pseudonym Avatar answered Nov 11 '22 22:11

Pseudonym