using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.FlexGrid;

namespace VerticalHeaders
{
    public class CustomCellFactory : CellFactory
    {
        public override void CreateColumnHeaderContent(C1FlexGrid grid, Border bdr, CellRange range)
        {
            // let base class start
            base.CreateColumnHeaderContent(grid, bdr, range);

            // rotate the content of headers for numeric and Boolean columns
            var type = grid.Columns[range.Column].DataType;
            if (type == typeof(double) || type == typeof(int) || type == typeof(bool))
            {
                // remove content from original cell
                var cellContent = bdr.Child as FrameworkElement;
                bdr.Child = null;

                // create layout transform to rotate header by 90 degrees
                //var lt = new System.Windows.Controls.LayoutTransformer(); // << this works too
                var lt = new C1.Silverlight.C1LayoutTransformer();
                lt.LayoutTransform = new RotateTransform() { Angle = -90 };

                // apply transform to content
                lt.Content = cellContent;
                cellContent.VerticalAlignment = VerticalAlignment.Center;

                // assign content back to cell
                bdr.Child = lt;
            }
        }
    }
}
