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 System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using Telerik.Windows.Controls;

namespace TreeViewDefImageSrc
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();
			this._TreeView.ItemsSource = this.GenerateData();

		}
		private ObservableCollection<DataItem> GenerateData()
		{
			ObservableCollection<DataItem> result = new ObservableCollection<DataItem>();

			for (int j = 1; j < 5; j++)
			{
				DataItem d = new DataItem()
				{
					Name = String.Format("Item {0}", j),
					ImagePath = String.Format("Image {0}", j)
				};
				for (int i = 1; i < 5; i++)
				{
					DataItem di = new DataItem()
					{
						Name = String.Format("SubItem {0}", i),
						ImagePath = String.Format("Image {0}", i)
					};
					d.Items.Add(di);
				}
				result.Add(d);
			}
			return result;
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			string path = "";

			foreach (DataItem item in _TreeView.ItemsSource)
			{
				foreach (DataItem subItem in item.Items)
				{
					path = item.ToString() + "|" + subItem.ToString();
					RadTreeViewItem subTreeViewItem = _TreeView.GetItemByPath(path, "|");
					subTreeViewItem.DefaultImageSrc = "images/" + item.ImagePath.ToString() + ".png";
				}

				path = item.ToString();
				RadTreeViewItem treeViewItem = _TreeView.GetItemByPath(path);
				treeViewItem.DefaultImageSrc = "images/" + item.ImagePath.ToString() + ".png";
			}
		}
	}
}
