I’m trying to bind a Label’s ‘Content’ property to a property from some custom type I have; unfortunately, I didn’t figure out how to do it, and that’s why I’m here 🙂
Let’s assume that I have the following type (can be in the same namespace as my WPF Window that contains the Label or different namespace):
namespace MyNS
{
public class Person
{
private int age = 0;
public int Age
{
get { return age; }
}
public void GetOlder
{
age++;
}
}
}
-
How to I bind my Label to ‘Age’ property?
-
At runtime I will create an instance of ‘Person’; I want to make sure that my Label is bound to the right instance; i.e. if I called:
Person SomePerson = new Person(); SomePerson.GetOlder();I want my Lable to have the new value of ‘Age’ property for ‘SomePerson’.
-
What if I called ‘GetOlder’ in different thread (whether using Dispatcher thread or BackgroundWorker)? Will I still get the latest value of ‘Age’? Or do I have to take care of some other things as well to make this scenario possible?
Thanks in advance,
TheBlueSky
It turned out to be kind of straightforward thing, I wonder why nobody answered it 🙂 Anyway, here are the answers:
First we need to create our Person class like this:
Then we bind our WPF control to Person.Age property like this:
Now whenever p.GetOlder() is called MyWpfLabelControl.Content will change to the new p.Age value.
In multi-threading, the story is not different; it’ll work the same way when calling p.GetOlder() in a different thread:
Hope this helps.
TheBlueSky