开发者

Bind WPF control with property of custom type

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++;
        }
    }
}
  1. How to I bind my Label to 'Age' property?

  2. 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'.

  3. 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:

  1. First we need to create our Person class like this:

    using System.ComponentModel;
    
    namespace MyNS
    {
        public class Person : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            private int age = 0;
    
            public int Age
            {
                get { return age; }
                set
                {
                    age = value;
                    OnPropertyChanged("Age");
                }
            }
    
            protected void OnPropertyChanged(string PropertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(PropertyName));
                }
            }
    
            public void GetOlder
            {
                Age++;
            }
        }
    }
    
  2. Then we bind our WPF control to Person.Age property like this:

    using System.Windows.Data;
    
    //...
    
    Person p = new Person();
    Binding ageBinding = new Binding("Age");
    ageBinding.Source = p;
    MyWpfLabelControl.SetBinding(Label.ContentProperty, ageBinding);
    

    Now whenever p.GetOlder() is called MyWpfLabelControl.Content will change to the new p.Age value.

  3. In multi-threading, the story is not different; it'll work the same way when calling p.GetOlder() in a different thread:

    new Thread(
        new ThreadStart(
            delegate() {
                p.GetOlder();
            }
    )).Start();
    

Hope this helps.

TheBlueSky

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜