We help IT Professionals succeed at work.

Binding a TextBlock's Text property in WPF

axnst2 asked
Hi Experts,

     I am learning WPF.  I was wondering if I can tie a TextBlock's Text property to the value of variable in my C# code and have the text update dynamically at run time real time?  Is this possible?  If yes, how?

Something simple like this:

<TextBlock x:Name="ServerConnectedTextBlock" Text="{myCSharpVariable}" />


int myCSharpVariable=0;

Watch Question

You can either set the property of the control from code, or bind the control to a property so it sees changes automatically. You can't bind it to a var.

If you want to do binding you need change notification.

This ms example shows creating a simple object that has properties that support notification. If you bound your control to an instance of this object.property and changed the object's property the control would see it.

Here's another writeup on change notification and binding that's just what you need to learn.