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

namespace SilverlightApplication1
{
    public class MyDataContext : ViewModelBase
    {
        ObservableCollection<MyDataRow> _data;
        public ObservableCollection<MyDataRow> Data
        {
            get
            {
                if (_data == null)
                {
                    var client = new Service1Client();
                    client.GetDataCompleted += (s, e) =>
                    {
                        _data = new ObservableCollection<MyDataRow>(from i in e.Result select 
                                                                        new MyDataRow(i));

                        OnPropertyChanged("Data");

                        IsLoading = false;
                    };

                    IsLoading = true;

                    client.GetDataAsync("select * from customers");
                }

                return _data;
            }
        }

        bool _isLoading;
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }
            set
            {
                if (_isLoading != value)
                {
                    _isLoading = value;

                    OnPropertyChanged("IsLoading");
                }
            }
        }
    }
}
