I update your code as follows, you could refer to it. Because I don't know your data content, I return the customized set data instead of the data in the file. I also used my RelayCommand class.
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Xml.Linq;
namespace LoadCollectionViewButtonBinding
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
class ProductPriceViewModel : INotifyPropertyChanged
{
private ICollectionView icv;
public ICollectionView View
{
get { return icv; }
set
{
icv = value;
OnPropertyChanged("View");
}
}
private Product selectedProduct;
public Product SelectedProduct
{
get { return selectedProduct; }
set
{
selectedProduct = value;
OnPropertyChanged("SelectedProduct");
}
}
public ObservableCollection<Product> Items { get; set; }
private void Execute(object parm)
{
var fe = (FrameworkElement)parm;
// var cvs = (CollectionViewSource)fe.FindResource("cvs");
Items = GetProductsPriceListXML();
View = CollectionViewSource.GetDefaultView(Items);
View.Refresh();
}
private ObservableCollection<Product> GetProductsPriceListXML()
{
var mylist = new ObservableCollection<Product>();
mylist.Add(new Product() { Name = "jack" });
mylist.Add(new Product() { Name = "john" });
mylist.Add(new Product() { Name = "marry" });
mylist.Add(new Product() { Name = "jenny" });
//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;
}
private readonly ICommand command;
public ICommand Command
{
get
{
return command;
}
}
public ProductPriceViewModel()
{
command = new RelayCommand(Execute);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Price { get; set; }
}
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
The result:
-
If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.