using System.Windows;
using Telerik.Windows.Controls;
using System.Windows.Data;
using Telerik.Windows.Controls.GridView;
using System.Windows.Media;
using System;
using System.Windows.Controls;
using System.Collections.Generic;

namespace ApplyCellStyleSelector
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			
			this.playersGrid.AutoGeneratingColumn += new System.EventHandler<GridViewAutoGeneratingColumnEventArgs>(playersGrid_AutoGeneratingColumn);		
		}

		

		void playersGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
		{

			if (e.Column.UniqueName == "Number")
			{
				//Styles
				// Negative Style
				Style negativeStyle = new Style();
				negativeStyle.TargetType = typeof(GridViewCell);

				Setter backGroundSetter = new Setter();
				backGroundSetter.Property = GridViewCell.BackgroundProperty;
				backGroundSetter.Value = Brushes.HotPink;
				negativeStyle.Setters.Add(backGroundSetter);

				// Positive Style
				Style normalStyle = new Style();
				normalStyle.TargetType = typeof(GridViewCell);

				//Selector and Rules
				ConditionalStyleSelector selector = new ConditionalStyleSelector();
				ConditionalStyleRule negativeRule = new ConditionalStyleRule();
				negativeRule.Style = negativeStyle;
				negativeRule.Value = false;
				ConditionalStyleRule normalRule = new ConditionalStyleRule();
				normalRule.Style = normalStyle;
				normalRule.Value = true;


				selector.Rules.Add(negativeRule);
				selector.Rules.Add(normalRule);
				NegativeValueConverter converter = new NegativeValueConverter();
				selector.ConditionConverter = converter;
				e.Column.CellStyleSelector = selector;
			}
		}

		private void Button1_Click(object sender, RoutedEventArgs e)
		{
		}

		private void Button2_Click(object sender, RoutedEventArgs e)
		{
		}
	}

	public class NegativeValueConverter : IValueConverter
	{
		#region IValueConverter Members
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			Player converterValue = (Player)value;

			if (converterValue != null)
			{
				return converterValue.Number > 10 ? true : false;
			}

			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
		#endregion
	}




	public class ConditionalStyleSelector : StyleSelector
	{
		public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container)
		{
			object conditionValue = this.ConditionConverter.Convert(item, null, null, null);
			foreach (ConditionalStyleRule rule in this.Rules)
			{
				if (Equals(rule.Value, conditionValue))
				{
					return rule.Style;
				}
			}


			return base.SelectStyle(item, container);
		}

		List<ConditionalStyleRule> _Rules;
		public List<ConditionalStyleRule> Rules
		{
			get
			{
				if (this._Rules == null)
				{
					this._Rules = new List<ConditionalStyleRule>();
				}

				return this._Rules;
			}
		}

		IValueConverter _ConditionConverter;
		public IValueConverter ConditionConverter
		{
			get
			{
				return this._ConditionConverter;
			}
			set
			{
				this._ConditionConverter = value;
			}
		}
	}

	public class ConditionalStyleRule
	{
		object _Value;
		public object Value
		{
			get
			{
				return this._Value;
			}
			set
			{
				this._Value = value;
			}
		}

		Style _Style;
		public Style Style
		{
			get
			{
				return this._Style;
			}
			set
			{
				this._Style = value;
			}
		}
	}
}
