WPF Binding to local variable

Published on Monday, November 14, 2011
wpf

Sample 1:

public string MyText {get;set;}

the binding is

{Binding MyText , RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}

Or

<Window Name="MyWindow" ...   
  <TextBlock Text="{Binding Text, ElementName=MyWindow}"/>

Sample 2:

<Window DataContext={Binding RelativeSource={RelativeSource Self}}>   

then the binding is

{Binding Text}