I wrote about this a bit in my blog post What is this "DataContext" you speak of?
To summarize, WPF applications have two layers: the UI layer and the data layer.
The data layer is the DataContext
, and bindings are used in the UI layer to access data from the data later.
When you write <TextBox Text="{Binding EmailAddress}" />
, you are telling WPF to look in the data layer (DataContext
) behind the TextBox
and bind to the EmailAddress
property.
The DataContext
is inherited down the visual tree, so typically you will only see the DataContext
set once, such as
public SomeConstructor()
{
this.InitializeComponent();
this.DataContext = ApplicationParameters.Instance;
}
It should be noted that you can also specify other binding properties to access values from somewhere other than the current DataContext
, such as ElementName
or Source
.
For example, since you are binding to a Singleton
property, you could use the following syntax for your binding:
Text="{Binding Path=EmailAddress,
Source={x:Static my:ApplicationParameters.Instance}}"
This would set the source of your binding to your singleton object, so it would bind to ApplicationParameters.Instance.EmailAddress
For this to work, you'll also have to specify the "my" namespace, like this:
<Window ... xmlns:my="clr-namespace:MyNamespace" />
But to make a long story short, the reason why your binding won't work is because the DataContext
behind your TextBlock
is not ApplicationParameters.Instance
, so it finds no EmailAddress
property to bind to :)