using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace SilverlightApplication1
{
    public class MyDataContext : INotifyPropertyChanged
    {
        ObservableCollection<MyObject> _Items;
        public ObservableCollection<MyObject> Items
        {
            get
            {
                if (_Items == null)
                {
                    _Items = new ObservableCollection<MyObject>();
                    _Items.CollectionChanged += (s, e) => { HasItems = _Items.Count > 0; };
                    (from i in Enumerable.Range(0, 10)
                     select new MyObject() { ID = i, Name = String.Format("Name{0}", i) }).ToList().ForEach(_Items.Add);

                }
                return _Items;
            }
            private set
            {
                if (_Items != value)
                {
                    _Items = value;

                    OnPropertyChanged("Items");
                }
            }
        }


        bool _HasItems;
        public bool HasItems
        {
            get
            {
                return _HasItems;
            }
            set
            {
                if (_HasItems != value)
                {
                    _HasItems = value;

                    OnPropertyChanged("HasItems");
                }
            }
        }

        public void Clear()
        {
            Items.Clear();
        }

        public void Reset()
        {
            Items = null;
        }


        #region INotifyPropertyChanged Members

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class MyObject
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
