using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Data;
using Telerik.Windows.Data;
using System.Globalization;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using Telerik.Windows.Controls;

namespace SilverlightDataTable
{
    public partial class Page : UserControl
    {
        string[] names = new string[] { "Côte de Blaye", "Boston Crab Meat", 
            "Singaporean Hokkien Fried Mee", "Gula Malacca", "Rogede sild", 
            "Spegesild", "Zaanse koeken", "Chocolade", "Maxilaku", "Valkoinen suklaa" };
        decimal[] prizes = new decimal[] { 23.25M, 9.00M, 45.60M, 32.00M, 
            14.00M, 19.00M, 263.50M, 18.40M, 3.00M, 14.00M };
        bool[] bools = new bool[] { true, false, true, false, true, false, true, false, true, false };
        DataTable table = null;

        private readonly Random rnd = new Random();

        public Page()
        {
            InitializeComponent();

            table = new DataTable();

            table.Columns.Add(new DataColumn() { ColumnName = "ID", DataType = typeof(int) });
            table.Columns.Add(new DataColumn() { ColumnName = "Image", DataType = typeof(BitmapImage) });         

            for (var i = 0; i < 10; i++)
            {
                DataRow row = table.NewRow();
                UpdateRowValues(row, i);

                table.Rows.Add(row);
            }

            RadGridView1.ItemsSource = table;
            RadGridView1.AutoGeneratingColumn += new EventHandler<Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs>(RadGridView1_AutoGeneratingColumn);
        }

        void RadGridView1_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            var column = e.Column as GridViewBoundColumnBase;
            if (column.DataType == typeof(BitmapImage))
            {
                e.Column = new GridViewImageColumn()
                {
                    UniqueName = column.UniqueName,
                    DataMemberBinding = column.DataMemberBinding               
                };
            }
        }

        private void UpdateRowValues(DataRow row, int i)
        {
            row["ID"] = i;
            row["Image"] = new BitmapImage() { UriSource = new Uri("red.png", UriKind.Relative) };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Update existing
            for (int i = 0; i < this.table.Rows.Count; i++)
            {
                this.UpdateRowValues(this.table.Rows[i], i);
            }

            //Add some new
            var row = this.table.NewRow();
            this.UpdateRowValues(row, this.table.Rows.Count);
            this.table.Rows.Add(row);
        }
    }

    public class MyCustomType : IComparable, IEquatable<MyCustomType>
    {
        public string MyProperty { get; set; }

        public override string ToString()
        {
            return MyProperty.ToString();
        }

        public bool Equals(MyCustomType other)
        {
            return MyProperty.Equals(other.MyProperty);
        }

        public int CompareTo(object obj)
        {
            return MyProperty.CompareTo(((MyCustomType) obj).MyProperty);
        }
    }
}
