Tutorial :WPF event that fires after element is fully parented, but before it's arranged


I'm writing a WPF control that dynamically changes its contents depending on what types of Window/UserControl descendants are in its parentage list (part of an experiment with convention vs. configuration).

As such, I need some code to run after my control is fully parented (i.e. all the way up to the Window that's being shown). Ideally, I'd also like my code to run before the first Measure/Arrange pass, since my code is going to change the control's contents and force another Measure/Arrange pass.

I've looked at EndInit, but it fires after the control is loaded from XAML, at which time it might not be fully parented. (For example, if my control was on a UserControl, then EndInit will fire once the UserControl is loaded -- but before it's parented to anything else. I want to wait until the UserControl is parented to something, and that's parented to something else, all the way up.)

Currently I'm just hooking the Loaded event from my control's constructor, and running my code there (oddly enough, WPF doesn't have an OnLoaded method to override):

public class MyControl  {      public MyControl()      {          Loaded += (sender, e) => { ... };      }  }  

This works -- it fires when the parents are fully populated -- but it's slightly less than optimal, because there's a Measure/Arrange pass that happens before Loaded.

Is there a good place I can put my code so that it runs after the Parents are set all the way up, but before the first Measure/Arrange pass?

Extra coolness points for solutions that would also work in Silverlight, ElementHost, the Blend/VS designer, and VisualBrush (i.e., not assuming that the top-level parent is a Window, or in the case of VisualBrush, not assuming that there even is a parent -- just that it's as parented as it's gonna be before showing up on the screen, or being sent to the printer, or whatever).


I believe the parents are all set in a single dispatcher operation, so you should be able to get that behavior by putting your logic in a delegate and queuing it up as the next dispatcher operation after the parent is set:

protected override void OnVisualParentChanged(DependencyObject oldParent)  {      base.OnVisualParentChanged(oldParent);      this.Dispatcher.BeginInvoke(new Action(OnReady));  }    private void OnReady()  {      // Element should be fully parented here  }  

You could also do that from EndInit rather than OnVisualParentChanged if you want to handle the case of no parent, although EndInit appears to be called more than once so you will need to check for duplicates:

private bool readyQueued;    public override void EndInit()  {      base.EndInit();      if (!readyQueued)      {          this.Dispatcher.BeginInvoke(new Action(OnReady));          readyQueued = true;      }  }    private void OnReady()  {      readyQueued = false;      // Element should be fully parented here  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »