0

In a WPF app I can move a UserControl from a ContentControl to another one in code like this:

myContentControl1.Content = null;
myContentControl2.Content = myUserControl;

The problem is that the UserControl that has been moved is a rather heavy one (consists of a lot of smaller controls). So the above operation takes a significant amount of time, almost as long as when I load the UserControl initially. It seems that every element in that UserControl goes through processing to be moved under another ContentControl.

Is there a more efficient and faster method of placing a UserControl under another parent (in my case a ContentControl) as its child (without the need for UI thread to process all its elements every time)? (Dynamically, in code-behind)

Community
  • 1
  • 1
rem
  • 16,745
  • 37
  • 112
  • 180
  • 1
    Please, give a reason for downvoting – rem Jan 30 '12 at 12:56
  • That's hard to answer without knowlegde about why you would do that. One thing of course is clear: changing the (logical) parent of the control will result in re-building its visual tree, including all the potentially expensive layout calculations. So why do you need to re-parent at all? Oh, i didn't downvote... – Clemens Jan 30 '12 at 12:56
  • @Clemens What I'm trying to achieve is to cash my UserControl somewhere in a non-visible part of UI, in order to make the visualizing my UserControls faster (I need constantly change them back and forth) – rem Jan 30 '12 at 13:02
  • Couldn't you simply move it out of the visible area by a TranslateTransform, by setting the control's RenderTransform property, or even simpler, set the control invisible? What about working with two controls like you do and while showing one, updating the other, invisible one in the background and then toggle both their visibility. – Clemens Jan 30 '12 at 13:05
  • Toggle their visibility between Visible and Hidden, not Collapsed since that would result in another layout cycle. – Clemens Jan 30 '12 at 13:11
  • Toggling the control Visible/Hidden is a perfectly fast process. The problem is that while I make a UserControl hidden I need to set the container (ContentControl) empty for another UserControl to be loaded in. I should operate with only one container. – rem Jan 30 '12 at 13:12

1 Answers1

2

Instead of switching a ContentControl's content between your two UserControl's, put both UserControl's in a Grid and toggle their Visibility. While showing one with current data, update the other, invisible one in the background. When updating is done, toggle both control's visibility from Visible to Hidden and vice versa. Don't use Collapsed, since that would force a new layout cycle.

Putting both in a Grid without specifying Row or Column makes them lie above each other.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • This is maybe the only way to go, if, as you have said in your comments, "changing the (logical) parent of the control will result in re-building its visual tree, including all the potentially expensive layout calculations". In fact, what I whanted to make clear for myself by asking this question is exactly: "is it possible to avoid re-building the visual tree?". If it's not possible, then I should perhaps try the way you suggested. (Though it's the way I was trying to avoid by looking for a way to move my Control under another parent.) Thanks! +1 – rem Jan 30 '12 at 14:17