INotifyPropertyChanged PropertyChangedEventHandler事件始终为null

时间:2011-03-24 15:01:33

标签: wpf inotifypropertychanged

我正在尝试使用WPF,并且在更新时遇到了一个小问题,主要是我在XML文件中正确更新新数据时显示旧数据。我已经实现了如下的INotifyPropertyChanged: -

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));
    }

}

然后更新如下: -

        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++;
        }

使用新的ProdName和Price正确保存XML,但是当我在更新后显示listView时,我仍然得到错误的值。

我绑定了这样的产品: -

    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();
    }

这是绑定代码: -

    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++;
        }

任何有关错误的帮助?

感谢您的帮助和时间

1 个答案:

答案 0 :(得分:1)

我真的没看到,循环中新创建的产品listView有什么关系。您不会将它们添加到列表中,也不会以其他方式将它们添加到listView 换句话说:在循环中创建这些实例是完全没用的,优化器将删除它们 您需要更新绑定到Products的列表中的listView类的实例。