I'm writing a custom Wpf control and I need to grab a reference to the containing window's HwndSource at the earliest possible time. This would be 1) in the constructor of my control if possible or 2) at the point when the control is added to the display hierarchy.
How can I detect when/if the HwndSource is available? I plan to acquire a reference using code such as the following:
var source = HwndSource.FromVisual(this) as HwndSource;
You can use the PresentationSource's AddSourceChangedHandler method to listen for when the PS changes (HwndSource is a derived PS).