But I am now producing these TabItems in code so need to reproduce this binding in my code-behind file. In this case HelpEditor is a custom UserControl with a PlantId dependency property and Symbol is a custom object with a Id dependency property. HelpEditor needs to be bound to the Id in Symbol and so this WPF binding code works fine.
Here is the code I am trying to use in my code-behind file:
TabItem ti = new TabItem();
ti.Header = "Notes";
helpEditor = new ScadaAlarmViewer.HelpEditor();
Binding idBinding = new Binding("Id");
idBinding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(MyStuff.Symbol), 1);
ti.Content = helpEditor;
But this doesn't work, I get:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MyStuff.Symbol', AncestorLevel='1''. BindingExpression:Path=Id; DataItem=null; target element is 'HelpEditor' (Name=''); target property is 'PlantId' (type 'String')
In the output window. If it's relevant, these TabItems are contained withint a Popup.