silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了How do I use WPF bindings with RelativeSource?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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?

link | edit | flag

67% accept rate
 
   

7 Answers

@H_618_132@

How do I use WPF bindings with RelativeSource?

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
@H_369_404@ @H_301_41@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_369_404@ @H_301_41@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 @H_290_447@mar 3 '09 at 9:31
Binding Relativesource={ 
   
Relativesource @H_120_153@mode=FindAncestor, AncestorType={x:Type ItemType} 
} 
... 

The default attribute of Relativesource is the @H_824_26@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
@H_369_404@ @H_301_41@1
 
Thanks for poitning out PrevIoUsData! Exactly what i was looking for! –  LnDCobra Sep 16 '10 at 9:14
@H_133_772@
@H_226_674@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
@H_369_404@ @H_301_41@1
 
nice overview,thanks for the link! –  David Schmitt @H_577_767@mar 15 '10 at 15:00

Don't forget TemplatedParent:

<Binding Relativesource=@H_265_801@"{Relativesource TemplatedParent}"/> 

or

{Binding Relativesource={Relativesource TemplatedParent}} 
link | edit | flag
 
   
@H_772_922@up vote 3 down vote

It's worthy of note that for those stuR_768_11845@bling 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 @H_20_992@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
  @H_673_1075@
   

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

How do I use WPF bindings with RelativeSource?

/>

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

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。