WPF Binding to local variable

The pattern is:

public string Text {get;set;}

and the binding is

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

If you want the binding to update automatically you should make it a DependencyProperty.


I think 3.5 added ElementName to bindings, so the following is a little easier:

<Window x:Name="Derp" ...
  <TextBlock Text="{Binding Text, ElementName=Derp}"/>

Leave a Comment