向Form添加新的PropertyChanged事件

时间:2011-10-27 10:15:44

标签: c# winforms events enums inotifypropertychanged

    public partial class FrmEditSiteParticulars : Form, INotifyPropertyChanged
    {

    public enum EntryTypes
    {
        Undefined,
        Site,
        Particular
    }

    private EntryTypes _EntryType;

    private EntryTypes EntryType { 
        get{return _EntryType;}
        set
        {
            if (value != _EntryType)
            {
                _EntryType = value;
                OnPropertyChanged("EntryType");
            }
        }
    }

    public event PropertyChangedEventHandler EntryTypeChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = EntryTypeChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
.
.
.
    public event PropertyChangedEventHandler PropertyChanged;
.
.
.

我加了

this.EntryTypeChanged += new 
     System.ComponentModel.PropertyChangedEventHandler(this.EntryType_Changed);

InitializeComponent方法内..


现在,当我打开设计师 enter image description here

我点击Ignore and Continue,效果很好..

现在当我再次关闭并打开解决方案时,我放在InitializeComponent中的eventHandler代码丢失了..

有什么问题?

1 个答案:

答案 0 :(得分:1)

看看这个:

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

答案是肯定的。将代码放在InitializeComponent();下面的构造函数中。