The converter is called while the visual tree is still assembled, so your Visual is not yet a child of the window.
You want to do the transformation after your visual tree is already built. This is done by registering the Dispatcher callback with Dispatcher.BeginInvoke(DispatcherPriority.Render, ...) and doing your work inside the callback.
This cannot be used with the converter because the converter must immediately return the converted value. A simple solution is to use an attached property instead of a converter. Here's how:
Suppose your binding looks like this:
<SomeObject Abc="{Binding Xyz, Converter={x:Static my:Converter.Instance}}" />
Create a subclass of DependencyObject "Independently" containing the attached property "AbcControl", whose PropertyChangedCallback performs the conversion and modifies the property "Abc":
public class AttachedProperties : DependencyObject { public Control GetAbcControl(... public void SetAbcControl(... ... AbcControlProperty = RegisterAttached("AbcControl", typeof(Control), typeof(AttachedProperties), new PropertyMetadata { PropertyChangedCallback = (obj, e) => { var ctrl = (Control)e.NewValue; Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => { var win = Window.GetWindow(ctrl); var transform = ctrl.TransformToAncestor(win);
Now you can write:
<SomeObject AbcControl="{Binding Xyz}" />
Which sets the Abc property to the converted value of Y.
There are many options in this general idea.
Ray burns
source share