微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐