using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Xml.Linq;
namespace WpfApp033
{
#pragma warning disable CS8601, CS8602, CS8612
class ProductPriceViewModel : INotifyPropertyChanged
{
public ProductPriceViewModel() { }
public List MainProductCategory
{
get => new List() {"PrintProduct","RetroPrint","PrintWithFrame","Calendar","Collage",
"CollagePoster","SingleSidePhoto","PassportPhoto","IDPhotoCard","GadgetProduct","GreetingCard"};
}
public List ProductName
{
get => new List() {
"10x10","10x15","10x8","10x20","13x18","13x13","15x20","15x23","20x25","20x30","Sticker","Metallic","Perfo" };
}
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 CollectionViewSource cvs = new CollectionViewSource();
public ICollectionView View
{
get
{
if (cvs.Source == null)
{
cvs.Source = GetProductsPriceList();
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); }
private void executemethod(object parameter)
{
switch (parameter.ToString())
{
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 GetProductsPriceList()
{
var mylist = new List();
data = XElement.Load("Window033Dta.txt");
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;
}
}
}
#pragma warning disable CS8767
public class RelayCommand : ICommand
{
private readonly Predicate