title: Finding draw order on WPF Canvas.Children Issue
body: <p>I needed to determine the draw order of the children contained in a canvas. So I ran across this Q/A:</p>
  
  <p><a href="https://stackoverflow.com/questions/8186844/how-to-compare-the-relative-z-order-of-two-wpf-controls-that-are-part-of-the-sam">How to compare the relative Z order of two WPF controls that are part of the same logical/visual tree?</a></p>
  
  <p>This works perfectly most of the time but sometimes I get an error from this code:</p>
  
  <pre><code>private Panel FindWindowRoot(FrameworkElement child)
  {
      FrameworkElement current = child;
      while(current as Window == null)
      {
          current = (FrameworkElement)VisualTreeHelper.GetParent(current); 
      }
      return ((Window)current).Content as Panel;
  }
  </code></pre>
  
  <p>The call <code>VisualTreeHelper.GetParent(current)</code> ends up throwing an exception "Value cannot be null."</p>
  
  <p>Here's one example of how I use the <code>DrawOrderComparer</code>.</p>
  
  <pre><code>ucVertexControl Control = new ucVertexControl(vertex);
  cnvDrawingArea.Children.Add(Control);
  SortedChildren = cnvDrawingArea.Children.OfType&lt;FrameworkElement&gt;().OrderByDescending(x =&gt; x, new Classes.DrawOrderComparer()).Cast&lt;UIElement&gt;().ToList();
  </code></pre>
  
  <p>My theory is that the sorting is occurring before the new control even has a parent defined because that gets set later by some event. Problem is I don't know what event that is and if I can listen for it.</p>
  
  <p>Anyone have any ideas?</p>