Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind the current instance of a control to an attached property

Tags:

binding

wpf

xaml

I'm looking to see if you can bind the current instance of a usercontrol or window to an attached property defined in its xaml, eg:

<Window MyAttachedProp.Value="{Binding Self}"/>
like image 395
deanvmc Avatar asked Aug 25 '12 14:08

deanvmc


2 Answers

You want the MyAttachedProp.Value to have the Window object reference?

You can use any of these methods:

  1. {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}

  2. give your Window an x:Name="XXXXX"...and then use {Binding ElementName=XXXXX} to find it.

  3. {Binding RelativeSource={RelativeSource Self}}

  4. {Binding RelativeSource={x:Static RelativeSource.Self}}

With example 4, it avoids the creation of a new RelativeSource object (with the Mode set to Self)...instead it points to the Static one already created in the RelativeSource class....(this is a VERY minor and premature optimization).

Most people use example 3 as it's less to type and clearer to read.

  • http://www.c-sharpcorner.com/uploadfile/yougerthen/relativesources-in-wpf/
like image 73
CSmith Avatar answered Sep 29 '22 04:09

CSmith


{Binding RelativeSource={RelativeSource Self}}

like image 44
H.B. Avatar answered Sep 29 '22 03:09

H.B.