How do I use WPF bindings with RelativeSource?

How do I use RelativeSource with WPF bindings and what are the different use-cases?

link | edit | flag

67% accept rate
 
  

7 Answers

up vote 71 down vote accepted

If you want to bind to another property on the object:

{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}} 

If you want to get a property on an ancestor:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}} 

If you want to get a property on the templated parent (so you can do 2 way bindings in a ControlTemplate)

{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}} 

or, shorter (this only works for OneWay bindings):

{TemplateBinding Path=PathToProperty} 
link | edit | flag
1  
When you say "bind to another property on the object", which object are you talking about? The display element or the object within the data context? –  Drew Noakes Dec 9 '08 at 17:42
1  
It turns out, as I revisit this, that the object in question is the object upon which the binding is being applied. I tried using <Button Tag="{Binding RelativeSource={RelativeSource Self}}" /> and looking in the debugger. The Tag property contained the button itself. –  Drew Noakes Mar 3 '09 at 9:31
Binding RelativeSource={ 
   
RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemType} 
} 
... 

The default attribute of RelativeSource is the Mode property. A complete set of valid values is given here (from MSDN):

  • PreviousData Allows you to bind the previous data item (not that control that contains the data item) in the list of data items being displayed.

  • TemplatedParent Refers to the element to which the template (in which the data-bound element exists) is applied. This is similar to setting a TemplateBindingExtension and is only applicable if the Binding is within a template.

  • Self Refers to the element on which you are setting the binding and allows you to bind one property of that element to another property on the same element.

  • FindAncestor Refers to the ancestor in the parent chain of the data-bound element. You can use this to bind to an ancestor of a specific type or its subclasses. This is the mode you use if you want to specify AncestorType and/or AncestorLevel.

link | edit | flag
1  
Thanks for poitning out PreviousData! Exactly what i was looking for! –  LnDCobra Sep 16 '10 at 9:14

Here is the cheat-sheet that does help not being confused: http://www.nbdtech.com/Free/WpfBinding.pdf

-aj

link | edit | flag
1  
nice overview, thanks for the link! –  David Schmitt Mar 15 '10 at 15:00

Don't forget TemplatedParent:

<Binding RelativeSource="{RelativeSource TemplatedParent}"/> 

or

{Binding RelativeSource={RelativeSource TemplatedParent}} 
link | edit | flag
 
  

It's worthy of note that for those stumbling across this thinking of Silverlight:

Silverlight offers a reduced subset only, of these commands

link | edit | flag
Yep, I was looking for SL support also. Vote it up: connect.microsoft.com/VisualStudio/feedback/details/480603/… –  TravisWhidden May 18 '10 at 18:31

I just posted another solution for accessing the DataContext of a parent element in Silverlight that works for me. It uses Binding ElementName.

link | edit | flag
 
  

Here's a more visual explanation in the context of a MVVM architecture: />

(http://www.rootsilver.com/2009/05/wpf-relativesource-ancestor-datacontext)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值