开发者

INotifyPropertyChanged PropertyChangedEventHandler event is always null

I am trying to get my hands on WPF, and I have encountered a small problem when updating, mainly that I am getting the old data displayed while the new data is correctly updated in the XML file. I have implemented INotifyPropertyChanged as follows :-

public class Products : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private string _prodId;
    public string ProdID
    {
        get { return _prodId; }
        set
        {
            _prodId = value;
            OnPropertyChanged("ProdID");
        }
    }

    private string _prodName;
    public string ProdName
    {
        get { return _prodName; }
        set
        {
            _prodName = value;
            OnPropertyChanged("ProdName");
        }
    }

    private string _prodPrice;
    public string ProdPrice
    {
        get { return _prodPrice; }
        set
        {
            _prodPrice = value;
            OnPropertyChanged("ProdPrice");
        }
    }


    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

}

and then the update is as follows :-

        foreach (XmlNode node in nodeList)
        {
            if (nodeList[i].ChildNodes[0].InnerText == strID)
            {
                Products products = new Products();
                products.ProdName = strName;
                products.ProdPrice = strPrice;
                nodeList[i].ChildNodes[1].InnerText = strName;
                nodeList[i].ChildNodes[2].InnerText = strPrice;
                break;
            }
            i++;
        }

The XML is being saved correctly with the new ProdName and Price, however when I display the listView after the update, i am still getting the wrong values.

I am binding the Products like this:-

    public static List<Products> LoadProduct()
    {
        string fileName = "Products.xml";
        List<Products> ListProdRecords = new List<Products>();
        // Execute the query using the LINQ to XML
        var prods = from p in XElement.Load(fileName).Elements("Product") select p;
        foreach (var product in prods)
        {
            Products lProduct = new Products
            {
                ProdID = product.Element("ProductId").Value,
                ProdName = product.Element("ProductName").Value,
                ProdPrice = product.Element("Price").Value
            };

            ListProdRecords.Add(lProduct);
        }
        return ListProdRecords.ToList();
    }

here is the binding code :-

    private void LoadProducts()
    {
        List<Products> productList = new List<Products>();
        productList = ProductDAL.LoadProduct();
        listView1.DataContext = productList;
    }

public static void UpdateProduct(string strID, string strName, string strPrice) { string fileName = "Products.xml";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(fileName);

        XmlNodeList nodeList = xmlDoc.SelectNodes("/Products/Product");
        int i = 0;
        foreach (XmlNode node in nodeList)
        {
            if (nodeList[i].ChildNodes[0].InnerText == strID)
            {
                Products products = new Products();
                products.ProdName = strName;
                products.ProdPrice = strPrice;
                nodeList[i].ChildNodes[1].InnerText = strName;
                nodeList[i].ChildNodes[2].InnerText = strPrice;
                break;
    开发者_运维问答        }
            i++;
        }

Any help on what's wrong?

Thanks for your help and time


I don't really see, what the newly created products in your loop have to do with a listView. You don't add them to a list or add them to the listView in another way.
Or in other words: The creation of those instances inside your loop is completely useless and will be removed by the optimizer.
You need to update the instances of the Products class that are in the list you bound to the listView.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜