I’ve been reading up on WPF data binding, looking for simple examples of how to bind, say, a string, to say, a TextBlock so when the value of the string changes so does the TextBlock..
Many web examples I’ve found look like this . . .
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new Person { FirstName="Rowan" };
}
}
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
}
… with the propertie(s) of the class being assigned in the “new” statement. So how do I set FirstName to something else later during runtime? Does doing a
this.DataContext = new Person { FirstName="Rowan" };
instantiate an object of type Person whose properties and methods can be accessed like any other object, the way that
Person a_person = new Person { FirstName="Rowan" };
…would? If so, then how do I access it?
Thanks in advance.
I believe you want something like this:
Although for your TextBlock to be notified of changes to the property your Person class will need to implement
INotifyPropertyChanged