How do I use WPF bindings with RelativeSource?

How do I use WPF bindings with RelativeSource?,第1张

概述How do I use RelativeSource with WPF bindings and what are the different use-cases? .net wpf databinding relativesource link | edit | flag edited Jan 21 '10 at 9:01 asked Sep 17 '08 at 15:10 David Sch

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

.net wpf databinding relativesource
link | edit | flag edited Jan 21 '10 at 9:01
asked Sep 17 '08 at 15:10

David Schmitt
15.1k 3 27 61
67% accept rate
 
   
@H_404_121@ 7 Answers active oldest votes
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 edited Jun 15 '10 at 16:26
answered Sep 17 '08 at 15:14

Abe Heidebrecht
5,160 6 17
@H_681_404@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
@H_681_404@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

up vote 20 down vote
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 answered Mar 3 '09 at 9:24

Drew Noakes
13.3k 3 43 90
@H_681_404@1
 
Thanks for poitning out PrevIoUsData! Exactly what i was looking for! –  LnDCobra Sep 16 '10 at 9:14
up vote 13 down vote

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

-aj

link | edit | flag answered Mar 14 '10 at 19:15

ajdotnet
131 1 3
@H_681_404@1
 
nice overvIEw,thanks for the link! –  David Schmitt Mar 15 '10 at 15:00
up vote 3 down vote

Don't forget TemplatedParent:

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

or

{Binding relativeSource={relativeSource TemplatedParent}} 
link | edit | flag answered Sep 17 '08 at 15:14

Bob King
6,316 1 13 41
 
   
up vote 3 down vote

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 answered Apr 24 '10 at 16:02

Matthew Black
31 1
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
up vote 2 down vote

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 answered Aug 23 '10 at 13:05

Juve
487 5 13
 
   
up vote 0 down vote

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

/>

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

@H_301_1173@ 总结

以上是内存溢出为你收集整理的How do I use WPF bindings with RelativeSource?全部内容,希望文章能够帮你解决How do I use WPF bindings with RelativeSource?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1078498.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存