订阅Winforms中的PropertyChangedEventHandler

时间:2015-09-08 00:05:17

标签: winforms .net-4.5 inotifypropertychanged

在以下示例中,RaisePropertyChanged()中的temp变量始终为null。我如何订阅活动?

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.CompilerServices;
using System.Text;

namespace TestProject.Module.BusinessObjects
{
    public class ContactPerson : INotifyPropertyChanged
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        [NotMapped]
        public string PersonFullName
        {
            set
            {
                if (PersonFullName != value)
                {
                    var stringArray = value.Split();
                    var firstIndex = 0;
                    var lastIndex = stringArray.Length - 1;

                    if (lastIndex >= firstIndex)
                    {
                        FirstName = stringArray[firstIndex];
                    }
                    if (lastIndex > firstIndex)
                    {
                        LastName = stringArray[lastIndex];
                    }
                    RaisePropertyChanged();
                }
            }
            get
            {
                var sb = new StringBuilder();

                sb.Append(FirstName);
                sb.Append(" ");

                sb.Append(LastName);

                var stringArray = sb.ToString().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                var s = string.Join(" ", stringArray);

                return s;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ContactPerson Clone()
        {
            var obj = new ContactPerson { FirstName = FirstName, LastName = LastName };

            return obj;
        }

        public override string ToString()
        {
            return PersonFullName;
        }

        protected void RaisePropertyChanged([CallerMemberName] string propertName = "")
        {
            var temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, new PropertyChangedEventArgs(propertName));
            }
        }
    }
}

从阅读This question看来,PropertyChanged似乎尚未订阅。我该如何订阅?

1 个答案:

答案 0 :(得分:2)

像这样:

    private void Form1_Load(object sender, EventArgs e)
    {
        ContactPerson p = new ContactPerson();
        p.PropertyChanged += P_PropertyChanged;
    }

    private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }

编辑:扩展示例代码:

public partial class Form1 : Form
{
    ContactPerson p;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        p = new ContactPerson();
        p.PersonFullName = "Mary Jane";
        p.PropertyChanged += P_PropertyChanged;

        label1.Text = p.PersonFullName;

        // If you use databinding instead, you get the same result in this case.
        //label1.DataBindings.Add(new Binding("Text", p, "PersonFullName"));
    }

    private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        label1.Text = p.PersonFullName;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        p.PersonFullName = "John Doe";
    }
}