-
November 16th, 2009, 04:55 AM
#1
Equivalent binding in code?
Hi,
I'm in the process of refactoring some code I've inherited and as a part of this I need to re-create a binding which was previously in my WPF in code. The WPF snippet is:
<TabItem Header="Notes">
<my:HelpEditor PlantId="{Binding Path=Id, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type my:Symbol}}}"/>
</TabItem>
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);
helpEditor.SetBinding(MyStuff.HelpEditor.PlantIdProperty, idBinding);
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.
Can anyone help?
Rich
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|