INotifyPropertyChanged PropertyChangedEventHandler 事件始终为 null
我正在尝试使用 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) { 字符串文件名 = "产品.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++;
}
有什么问题可以帮忙吗?
感谢您的帮助和时间
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的不明白循环中的新创建的产品与
listView
有什么关系。您不会将它们添加到列表中或以其他方式将它们添加到listView
中。或者换句话说:在循环内创建这些实例是完全无用的,将被优化器删除。
您需要更新绑定到
listView
的列表中的Products
类的实例。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 thelistView
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 thelistView
.