Detect when WPF textbox finished rendering after pasting text

I have a small program written to manipulate text data and then write to a text file. Typical workflow:

  • Open an Excel file, select and copy the entire worksheet.
  • Paste data into a multiline text box in my WPF application.
  • Select your options, hit "Export" and let it do it.

Here's the problem. When inserting a large amount of data (600,000 rows, etc.), it takes some time until the data appears in the text box, even if the insert event occurs immediately. I am guessing the lag is a rendering issue.

When the paste event fires, I pop up a status window to alert the user that the paste has started.

I want to know when the paste / redraw has finished, so I can close the status window , but couldn't find any information on the matter.

I was probably looking for the wrong stuff, as all I can find is working around to get the windows to redraw before continuing with the code. This seems like an ugly hack. I would rather just listen for the "render completed" event. Maybe there is nothing like it.

+3


source to share


1 answer


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

WPF is full of surprises. This simplifies tasks, but at the same time makes the task too complicated. A good example is tricky things like running code when you are sure the windows are finished rendering. The event loaded by the window does not always work because the controls may still be displayed. I had this problem working with Infragistics XamDockManager. It continued to render widgets even when the Window Loaded event was raised. Unfortunately, there is no "official" solution for this problem.

But there is a trick. You can execute your asynchronous code using the Dispatcher Class.



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

      

This code must be added to the Window Loaded event handler. this is done when all the controls inside your window are displayed. It seems the address of your problem

+2


source







All Articles