using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Telerik.Windows.Controls;

namespace ControllingExpanderStyle_WPF
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private SampleViewModel sampleVM;
		public static Style Style1;
		public static Style Style2;
		public static Style Style3;

		public MainWindow()
		{
			InitializeComponent();

			sampleVM = new SampleViewModel();
			myTreeView.ItemsSource = sampleVM;

			Style1 = Resources["ExpanderStyle_1"] as Style;
			Style2 = Resources["ExpanderStyle_2"] as Style;
			Style3 = Resources["ExpanderStyle_3"] as Style;
			
			//Set the style from the view model.
			//this.sampleVM[0].MyCustomExpanderStyle = Style3;
		}

		private void TreeStyle1_Click(object sender, RoutedEventArgs e)
		{
			this.myTreeView.ExpanderStyle = Style1;
		}

		private void TreeStyle2_Click(object sender, RoutedEventArgs e)
		{
			this.myTreeView.ExpanderStyle = Style2;
		}

		private void TreeStyle3_Click(object sender, RoutedEventArgs e)
		{
			this.myTreeView.ExpanderStyle = Style3;
		}

		private void ClearLocalTreeStyle_Click(object sender, RoutedEventArgs e)
		{
			this.myTreeView.ClearValue(RadTreeView.ExpanderStyleProperty);
		}

		private void ItemStyle1_Click(object sender, RoutedEventArgs e)
		{
			//Use the visual container
			var selectedContainer = this.myTreeView.SelectedContainer;
			if (selectedContainer != null)
			{
				selectedContainer.ExpanderStyle = Style1;
			}

			//Using the data item.
			//var selectedItem = this.myTreeView.SelectedItem as BusinessItem;
			//if (selectedItem != null)
			//{
			//    selectedItem.MyCustomExpanderStyle = Style1;
			//}
		}

		private void ItemStyle2_Click(object sender, RoutedEventArgs e)
		{
			var selectedContainer = this.myTreeView.SelectedContainer;
			if (selectedContainer != null)
			{
				selectedContainer.ExpanderStyle = Style2;
			}
		}

		private void ItemStyle3_Click(object sender, RoutedEventArgs e)
		{
			var selectedContainer = this.myTreeView.SelectedContainer;
			if (selectedContainer != null)
			{
				selectedContainer.ExpanderStyle = Style3;
			}
		}

		private void ClearLocalItemStyle_Click(object sender, RoutedEventArgs e)
		{
			var selectedContainer = this.myTreeView.SelectedContainer;
			if (selectedContainer != null)
			{
				selectedContainer.ExpanderStyle = null;
				selectedContainer.ClearValue(RadTreeViewItem.ExpanderStyleProperty);
			}
		}
	}

	public class SampleViewModel : ObservableCollection<BusinessItem>
	{
		public SampleViewModel()
		{
			for (int i = 0; i < 100; i++)
			{
				BusinessItem bi = new BusinessItem()
				{
					Name = i.ToString(),
					Level = 1
				};

				for (int j = 0; j < 3; j++)
				{
					BusinessItem bi2 = new BusinessItem()
					{
						Name = bi.Name + "." + j,
						Level = 2
					};

					for (int k = 0; k < 3; k++)
					{
						BusinessItem bi3 = new BusinessItem()
						{
							Name = bi2.Name + "." + k,
							Level = 3
						};
						bi2.Items.Add(bi3);

						for (int l = 0; l < 3; l++)
						{
							BusinessItem bi4 = new BusinessItem()
							{
								Name = bi3.Name + "." + l,
								Level = 4
							};
							bi3.Items.Add(bi4);
						}
					}

					bi.Items.Add(bi2);
				}

				this.Add(bi);
			}
		}
	}

	public class BusinessItem : INotifyPropertyChanged
	{
		public override string ToString()
		{
			return this.Name;
		}

		public BusinessItem()
		{
			Items = new ObservableCollection<BusinessItem>();
		}

		public ObservableCollection<BusinessItem> Items { get; set; }

		public string Name { get; set; }

		public int Level { get; set; }

		private Style myCustomExpanderStyle;
		public Style MyCustomExpanderStyle
		{
			get
			{
				return myCustomExpanderStyle;
			}
			set
			{
				if (value != myCustomExpanderStyle)
				{
					myCustomExpanderStyle = value;
					OnPropertyChanged("MyCustomExpanderStyle");
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
