I currently have this line of code which I want to work in all cases:
var visualWindowContent = (UIElement)window.Content;
This approach will work when Window.Content is a UIElement. But what about when it's a non-visual object which then has a DataTemplate applied to it? The above line of code would throw a bad cast exception. So how to get the window's visual content in that case?
EDIT: At first I said that VisualTreeHelper.GetChild(window, 0) returned null, but it was in fact non-null. My purpose here is to get the root adorner layer by passing visualWindowContent to AdornerLayer.GetAdornerLayer. It turns out that was failing (returning null) when passed the window's immediate visual child since that node wasn't deep enough in the visual tree, i.e. a descendant of AdornerDectorator.
Using the FindVisualChild method in the page linked by @ReedCopsey, this appears to work:
var contentPresenter = FindVisualChild<ContentPresenter>( window );
var visualWindowContent = (UIElement)VisualTreeHelper.GetChild( contentPresenter, 0 );