using System;
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 System.ComponentModel;
using C1.Silverlight;

namespace C1_Demo
{
    // Represents a custom TypeConverter used to convert the item headers from the sample
    // to a String representation so we can make use of the autosearch and find operations.
    public class CustomItemConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (destinationType.IsAssignableFrom(typeof(string)))
            {
                C1TreeViewItem itemValue = value as C1TreeViewItem;
                if ((itemValue != null) && (itemValue.Header != null))
                {
                    StackPanel customHeader = itemValue.Header as StackPanel;
                    TextBlock text = customHeader.Children[1] as TextBlock;
                    return text.Text;
                }
                else
                {
                    return value.ToString();
                }
            }
            else
            {
                return null;
            }
        }
    }
}
