开发者

C# Custom Class using INotifyPropertyChanged not notifying on Assignment

I have a class that implements the INotifyPropertyChanged interface. If I create a new instance of the object, the PropertyChanged event gets set after I retrieve a value from it.

Example:

MyItem itm = new MyItem();  //MyItem.PropertyChanged == null
string test = itm.Value;    //MyItem.PropertyChanged != null

If I assign itm the value of another MyItem, the PropertyChanged event remains null.

Example:

itm = (MyItem)cboMyItemsCombobox.SelectedItem; // Properties for itm change to the values 
                                               // of the selected item, but PropertyChanged 
                                               // == null

I believe the problem lies partially in my custom constructor for the class, but I'm not entirely sure.

The goal is to have a variable to hold data for a record, called mnuitm, that is bound to 3 textbox objects. When the text in a textbox changes, the change is made to the property in mnuitm. When the property in mnuitm is changed, the change is made in the textbox. This works if I create a new MenuItem and assign the values individually, but does not work if I assign an already populated MenuItem to mnuitm.

Here is my actual code for (hopefully) more clearity on the issue.

public partial class frmMenuItems : Form
{
    private class MenuItem : INotifyPropertyChanged
    {
        private Int32 mid;
        private string txt;
        private string url;
        private string scp;

        public MenuItem() { }

        public MenuItem(Int32 id, string txt, string url, string scp)
        {
            ID = id;
            Text = txt;
            URL = url;
            Script = scp;
        }

        public Int32 ID
        {
            get
            {
                return mid;
            }
            set
            {
                if (mid != value)
                {
                    mid = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        public string Text {
            get
            {
                return txt;
            }
            set
            {
                if (txt != value)
                {
                    txt = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }

        public string URL {
            get
            {
                return url;
            }
            set
            {
                if (url != value)
                {
                    url = value;
                    NotifyPropertyChanged("URL");
                }
            }
        }

        public string Script {
            get
            {
                return scp;
            }
            set
            {
                if (scp != value)
                {
                    scp = value;
                    NotifyPropertyChanged("Script");
                }
            }
        }

        public void Clear()
        {
            ID = 0;
            Text = "";
            URL = "";
            Script = "";
        }

        public override string ToString()
        {
            return Text;
        }

        private void NotifyPropertyChanged(string inf)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(inf));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }


    private MenuItem mnuitm;
    private MySqlConnection sqlcon;

    public frmMenuItems()
    {
        InitializeComponent();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        mnuitm.Clear();
    }

    private void frmMenuItems_Load(object sender, EventArgs e)
    {
        string constr = "server={0};uid={1};pwd={2};database={3};";
        DBItem dbi = CountyDataManager.CountyData.DBConnection;
        constr = string.Format(constr, [MyHost], [MyUsername], [MyPassword], [MyDatabase]);
        sqlcon = new MySqlConnection(constr);
        sqlcon.Open();

        mnuitm = new MenuItem();
        SetBindings();
        RefreshList();
    }

    private void SetBindings()
    {
        txtMenuText.DataBindings.Clear();
        txtURL.DataBindings.Clear();
        txtScript.DataBindings.Clear();

        txtMenuText.DataBindings.Add("Text", mnuitm, "Text");
        txtURL.DataBindings.Add("Text", mnuitm, "URL");
        txtScript.DataBindings.Add("Text", mnuitm, "Script");
    }

    private void RefreshList()
    {
        using (MySqlCommand cmd = new MySqlCommand("SELECT `menuid`,`menutext`,`url`,`script` FROM tblindexmenu ORDER BY `menutext`", sqlcon))
        {
            lstMenuItems.Items.Clear();
            using (MySqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    lstMenuItems.Items.Add(new MenuItem(Int32.Parse(rdr[0].ToString()), rdr[1].ToString(),rdr[2].ToString(),rdr[3].ToString()));
                }
            }
        }
    }

    private void frmMenuItems_FormClosing(object sender, FormClosingEventArgs e)
    {
        sqlcon.Close();
    }

    private void lstMenuItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lstMenuItems.SelectedIndex > -1)
        {
            mnuitm = (MenuItem)lstMenuItems.SelectedItem;
        }
    }
}   

After receiving feedback, I made the following changes:

Added CopyFrom() to MenuItem

public void CopyFrom(MenuItem itm)
{
    th开发者_运维百科is.ID = itm.ID;
     this.URL = itm.URL;
     this.Text = itm.Text;
     this.Script = itm.Script;
}

I then modified the SelectedIndexChanged code to use the new function

mnuitm.CopyFrom((MenuItem)lstMenuItems.SelectedItem);


This is by design. When you write

itm = (MyItem)cboMyItemsCombobox.SelectedItem;

you haven't changed any of the properties of the MenuItem itm used to point to, rather you changed the MenuItem itm points to.

One option for what you need is add a function to MenuItem that looks like

SetFromOtherMenuItem(MenuItem other)
{
    this.Url = other.Url
    //etc
}

Again, PropertyChanged means that a property on some instance has changed. In your case, only one of the variables pointing to that instance changed (to point to a different instance).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜