using System;
using System.Collections.ObjectModel;
using System.Linq;
using Telerik.Windows.Controls;

namespace ListBoxExample
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            LineItems = new ObservableCollection<LineItem>();
            RefreshLineItems();
        }
        public void RefreshLineItems()
        {
            // Store the selected article for later use.
            LineItem selectedLineItem = null;
            if (SelectedArticle != null)
                selectedLineItem = SelectedArticle;

            // Clear all the line items.
            LineItems.Clear();
            // Iterate through the cart to re-populate LineItems and reset the selected article.
            for (int i = 0; i < 10; i++)
            {
                LineItems.Add(new LineItem() { Name = Guid.NewGuid().ToString(), articleID = i });
            }
                if (selectedLineItem != null)
                    SelectedArticle = LineItems.FirstOrDefault(x => x.articleID == selectedLineItem.articleID);
        }
        public ObservableCollection<LineItem> LineItems { get; set; }
        private LineItem selectedArticle;
              
        public LineItem SelectedArticle
        {
            get { return selectedArticle; }
            set { selectedArticle = value; this.OnPropertyChanged("SelectedArticle"); }
        }
    }
    public class LineItem
    {
        public int articleID { get; set; }
        public string Name { get; set; }
    }
}
