Answered by:
Binding Relative Source in code Behind

Question
-
Hi all,
I create a custom DP in usercontrol, how can i binding it in code behind using Relative Source? In MSDN i cannot find any example about binding Ralatice source in code behind.
Thanks
Saturday, April 9, 2011 5:57 AM
Answers
-
You can create the binding, then set its RelativeSource property. It's just like creating a normal binding, and adding that.
The options for the different sources (ie: AncestorType/Level/etc) are part of the RelativeSource class. There is a C# example here.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked as answer by Min Zhu Wednesday, April 13, 2011 3:11 AM
Saturday, April 9, 2011 6:14 AM -
Hi hungdoan,
You use RelativeSourceMode.Self mode in your code, which makes MyLable bind to itself.
Change the mode to FindAncestor to fix the problem.
Binding b = new Binding(); b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,this.GetType(),1); b.Path = new PropertyPath("MyDP"); MyLable.SetBinding(ContentProperty, b);
If you still have any questions or concerns about this issue, please feel free to let me know.
Best regards,
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by hungdoan Wednesday, April 13, 2011 2:33 AM
Monday, April 11, 2011 8:26 AM
All replies
-
You can create the binding, then set its RelativeSource property. It's just like creating a normal binding, and adding that.
The options for the different sources (ie: AncestorType/Level/etc) are part of the RelativeSource class. There is a C# example here.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked as answer by Min Zhu Wednesday, April 13, 2011 3:11 AM
Saturday, April 9, 2011 6:14 AM -
I create one custom control named ControlDisplay() inluding one custom dp named MyDP and bind it to MyLable content. However when i changed MyDP, the MyLable not update. Here are my code:
public ControlDisplay()
{
this.InitializeComponent();
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
b.Path = new PropertyPath("MyDP");
MyLable.SetBinding(ContentProperty, b);
}
If i bind using XAML, everything's fine. How can i do it in code behind?- Proposed as answer by Drew Hudec Friday, July 12, 2013 1:40 PM
Saturday, April 9, 2011 7:55 AM -
Hi hungdoan,
You use RelativeSourceMode.Self mode in your code, which makes MyLable bind to itself.
Change the mode to FindAncestor to fix the problem.
Binding b = new Binding(); b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor,this.GetType(),1); b.Path = new PropertyPath("MyDP"); MyLable.SetBinding(ContentProperty, b);
If you still have any questions or concerns about this issue, please feel free to let me know.
Best regards,
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by hungdoan Wednesday, April 13, 2011 2:33 AM
Monday, April 11, 2011 8:26 AM -
thank you very much
Wednesday, April 13, 2011 2:34 AM