Button click:
Datagrid:
class ProductPriceViewModel : INotifyPropertyChanged
{
public ProductPriceViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler? CanExecuteChanged;
public void OnPropertyChanged([CallerMemberName] String info = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
private bool Filter(object item)
{
Product p = item as Product;
if (p == null) return true;
var ret = true;
if (!String.IsNullOrEmpty(MainProductSearch))
ret = ret && p.Mainproduct.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0 ||
p.Name.IndexOf(MainProductSearch, StringComparison.OrdinalIgnoreCase) >= 0;
if (!String.IsNullOrEmpty(SizeSearch))
ret = ret && p.Name.IndexOf(SizeSearch, StringComparison.OrdinalIgnoreCase) >= 0;
if (!String.IsNullOrEmpty(MediaType))
ret = ret && p.Name.IndexOf(MediaType, StringComparison.OrdinalIgnoreCase) >= 0;
if (Visible.HasValue)
ret = ret && p.Visible.IndexOf(Visible.Value.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
return ret;
}
private ICollectionView cvs;
public ICollectionView View
{
get { return cvs; }
set
{
cvs = value;
OnPropertyChanged("View");
}
}
private readonly ICommand command;
public ICommand Command
{
get
{
return command;
}
}
//private CollectionViewSource cvs = new CollectionViewSource();
// public ICollectionView View { get; set; }
//public ICollectionView View
//{
// get
// {
// if (cvs.Source == null)
// {
// cvs.Source = GetProductsPriceListXML();
// cvs.View.Filter = Filter;
// }
// return this.cvs.View;
// }
//}
public Product SelectedProduct { get; set; }
private string _MainProductSearch;
public string MainProductSearch
{
get { return _MainProductSearch; }
set
{
_MainProductSearch = value;
OnPropertyChanged();
View.Refresh();
}
}
private string _SizeSearch;
public string SizeSearch
{
get { return _SizeSearch; }
set
{
_SizeSearch = value;
OnPropertyChanged();
View.Refresh();
}
}
private string _MediaType;
public string MediaType
{
get { return _MediaType; }
set
{
_MediaType = value;
OnPropertyChanged();
View.Refresh();
}
}
private bool? _Visible;
public bool? Visible
{
get { return this._Visible; }
set
{
this._Visible = value;
OnPropertyChanged();
View.Refresh();
}
}
public ICommand MyCommand { get => new RelayCommand(executemethod, canexecutemethod); }
public ICommand ReadCommand { get => new RelayCommand(Execute, canexecutemethod); }
string pricinglocalfile = @"C:\xmltest\Prices_2022_12_15_All_CM_V4.txt";
private void Execute(object parm)
{
var fe = (FrameworkElement)parm;
Items = GetProductsPriceListXML();
View = CollectionViewSource.GetDefaultView(Items);
View.Refresh();
}
private void executemethod(object parameter)
{
switch (parameter.ToString())
{
case "ExcelExport":
ExcelUtlity obj = new ExcelUtlity();
System.Data.DataTable dt = ConvertToDataTable(GetProductsPriceListXML());
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "Price_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx";
obj.WriteDataTableToExcel(dt, path);
System.Windows.MessageBox.Show("Excel Pricing File created on the desktop");
return;
case "SaveFile":
data.Save("xxx.txt");
return;
case "Reset":
MainProductSearch = string.Empty;
SizeSearch = string.Empty;
SizeSearch = string.Empty;
Visible = null;
MediaType = string.Empty;
break;
default:
MediaType = parameter.ToString();
break;
}
View.Refresh();
}
private static bool canexecutemethod(object obj) => true;
XElement data;
private List GetProductsPriceListXML()
{
var mylist = new List();
data = XElement.Load(pricinglocalfile);
foreach (XElement xe1 in data.Elements())
if (xe1.Name == "Products")
foreach (var xe2 in xe1.Elements()) mylist.Add(new Product(xe2));
return mylist;
}
}
public class MediaConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> value != null && parameter != null && value.ToString() == parameter.ToString();
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
=> parameter;
}
public class Product
{
public Product(XElement xe3) => this.xe4 = xe3;
private XElement xe4;
public string Mainproduct { get => xe4.Name.LocalName; }
public string Name { get => xe4.Attribute("Name").Value; }
public string Price
{
get
{
XElement xe5 = xe4.Descendants("ProductPrice").FirstOrDefault();
if (xe5 == null) return string.Empty;
return xe5.Attribute("Price").Value;
}
set
{
XElement xe5 = xe4.Descendants("ProductPrice").FirstOrDefault();
if (xe5 != null) xe5.Attribute("Price").Value = value;
}
}
public string Visible
{
get
{
XElement xe5 = xe4.Descendants("ProductVisibility").FirstOrDefault();
if (xe5 == null) return string.Empty;
return xe5.Attribute("Visible").Value;
}
set
{
XElement xe5 = xe4.Descendants("ProductVisibility").FirstOrDefault();
if (xe5 != null) xe5.Attribute("Visible").Value = value;
}
}
public string NameIcon
{
get
{
XAttribute xe5 = xe4.Attribute("DefaultIconName");
return (xe5 == null) ? string.Empty : xe5.Value;
}
set
{
XAttribute xe5 = xe4.Attribute("DefaultIconName");
if (xe5 == null) xe4.Add(new XAttribute("DefaultIconName", value));
else xe5.Value = value;
}
}
}
public class RelayCommand : ICommand
{
private readonly Predicate