
using System.Windows;
using System.Windows.Resources;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using Telerik.Windows.Data;
using System.Globalization;
using System;


namespace SilverlightCustomFiltering
{
	public partial class Page : UserControl
	{
		public Page()
		{
			InitializeComponent();
			this.radGridViewFirstLook.ItemsSource = new[] { 
			                                    new Customer { CompanyName = "Telerik" },
			                                    new Customer { CompanyName = "Microsoft"},
			                                    new Customer { CompanyName = "SAP"},
			                                    new Customer { CompanyName = "Other" },
												new Customer { CompanyName = "AlaBala" }
			                                  };
		}
		private void TextBoxSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
			TextBox txtBox = (TextBox)sender;
			if (txtBox.Text == String.Empty)
			{
				this.radGridViewFirstLook.FilterDescription = null;
			}
			else
			{
				this.radGridViewFirstLook.FilterDescription = new CustomFilterDescription(txtBox.Text);
			}
			
		}

	}

	public class Customer
	{
		public string CompanyName
		{
			get;
			set;
		}
	}
	public class CustomFilterDescription : FilterDescription
	{
		string filterValue;
		string rowHeaderToFilter;
		bool caseSensitive;

		public CustomFilterDescription(string filter)
		{
			this.filterValue = filter;
			this.rowHeaderToFilter = "CompanyName";
			this.caseSensitive = false;
		}
		public override bool SatisfiesFilter(object dataItem)
		{
			Customer row = (Customer)dataItem;

			if (String.IsNullOrEmpty(filterValue))
				return false;

			if (row == null)
				return false;

			string cellCompareValueNormalized;
			string filterValueNormalized;

			if (this.caseSensitive)
			{
				cellCompareValueNormalized = row.CompanyName.ToString();
				filterValueNormalized = filterValue;
			}
			else
			{
				cellCompareValueNormalized = row.CompanyName.ToString().ToLower(CultureInfo.InvariantCulture);
				filterValueNormalized = filterValue.ToLower(CultureInfo.InvariantCulture);
			}

			if (cellCompareValueNormalized != null && cellCompareValueNormalized.IndexOf(filterValueNormalized) >= 0)
				return true;

			return false;
		}
	}
}
