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 C1.Silverlight.FlexGrid;
using System.ComponentModel.DataAnnotations;

namespace GridTooltips
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            _grid.CellFactory = new ToolTipCellFactory();
            var list = new List<Customer>();
            for (int i = 0; i < 50; i++)
            {
                list.Add(new Customer() { 
                    Name = "Customer " + i.ToString(), 
                    Age = 10 + i, 
                    Active = i % 3 != 0 });
            }
            _grid.ItemsSource = list;

            foreach (Column col in _grid.Columns)
            {
                col.Tag = string.Format("tag for column '{0}'", col.ColumnName);
            }

            // hook up event handler
            _grid.PrepareCellForEdit += _grid_PrepareCellForEdit;
        }

        // customize editor by changing the appearance of the selection
        void _grid_PrepareCellForEdit(object sender, CellEditEventArgs e)
        {
            var b = e.Editor as Border;
            var tb = b.Child as TextBox;
            if (tb != null)
            {
                tb.SelectionBackground = new SolidColorBrush(Colors.Blue);
                tb.SelectionForeground = new SolidColorBrush(Colors.Yellow);
            }
        }
    }

    /// <summary>
    /// Custom cell factory class that adds a tooltip to the cells.
    /// </summary>
    public class ToolTipCellFactory : CellFactory
    {
        public override void CreateCellContent(C1FlexGrid grid, Border bdr, CellRange rng)
        {
            // crate the content
            base.CreateCellContent(grid, bdr, rng);

            // add the tooltip
            var tip = string.Format("row: {0} col: {1}\r\ncontent: {2}\r\ncolumn Tag: {3}",
                rng.Row,
                rng.Column,
                grid[rng.Row, rng.Column],
                grid.Columns[rng.Column].Tag);
            ToolTipService.SetToolTip(bdr, tip);
        }
    }

    /// <summary>
    /// Simple data class.
    /// </summary>
    public class Customer
    {
        [Display(Name = "Name")]
        public string Name { get; set; }

        [Display(Name = "Age")]
        public int Age { get; set; }

        [Display(Name = "Active")]
        public bool Active { get; set; }
    }
}
