using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Xml;
using System.Xml.Serialization;
namespace WpfApp040
{
class ProductPriceViewModel : INotifyPropertyChanged
{
// ctor
public ProductPriceViewModel() => ProductPriceList = new ObservableCollection();
// Path of xml file
public string FilePath { get; set; } = @"C:\temp\xDta.txt";
// View member
private CollectionViewSource cvs = new CollectionViewSource();
public ICollectionView View { get => cvs.View; }
private ObservableCollection _productPriceList;
private ObservableCollection ProductPriceList { get => this._productPriceList;
set
{
this._productPriceList= value;
cvs.Source= value;
OnPropertyChanged(nameof(View));
}
}
// Commands
public ICommand Cmd { get => new RelayCommand(executemethod, canexecutemethod); }
private void executemethod(object obj)
{
switch (obj.ToString())
{
case "Save":
SaveProductsPriceList();
break;
case "Load":
ProductPriceList = GetProductsPriceList();
break;
default:
break;
}
}
private static bool canexecutemethod(object obj) => true;
#pragma warning disable CS8601 // Dereference of a possibly null reference.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
private void SaveProductsPriceList() //to read xml fiel contents
{
var serializer = new XmlSerializer(typeof(ObservableCollection));
using (var writer = new XmlTextWriter(FilePath, Encoding.UTF8))
serializer.Serialize(writer, ProductPriceList);
}
#pragma warning disable CS8603 // Possible null reference return.
private ObservableCollection GetProductsPriceList() //to read xml fiel contents
{
var serializer = new XmlSerializer(typeof(ObservableCollection));
using (Stream str = new FileStream(FilePath, FileMode.Open))
return (ObservableCollection)serializer.Deserialize(str);
// old wrong version
//var mylist = new ObservableCollection();
//XmlDocument doc = new XmlDocument();
//doc.Load(FilePath);
//foreach (XmlElement pn in doc.SelectNodes("/Products"))
//{
// var productlist = new Products
// {
// Mainproduct = pn.LocalName.ToString(),
// Name = pn.GetAttribute("Name"),
// Price = pn.SelectSingleNode(".//ProductPrice/@Price")?.Value,
// Visible = pn.SelectSingleNode(".//ProductVisibility/@Visible")?.Value,
// NameIcon = pn.GetAttribute("DefaultIconName")
// };
// mylist.Add(productlist);
//}
//return mylist;
}
// INotifyPropertyChanged
#pragma warning disable CS8612 // Nullability of reference types in type doesn't match implicitly implemented member.
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String info) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
public class Products
{
public string Mainproduct { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public string Visible { get; set; }
public string NameIcon { get; set; }
}
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
public class RelayCommand : ICommand
{
private readonly Predicate