using System;
using System.Collections.Generic;
using System.Linq;
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;
using System.Reflection;
using System.ComponentModel;

namespace Templates
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            // get list of items
            IEnumerable list = GetItems();

            // add items to ListBox and in C1ComboBox
            _listBox.ItemsSource = list;
            _cmb1.ItemsSource = list;

            // show fonts in another C1ComboBox
            FontFamily[] ff = new FontFamily[]
            {
               new FontFamily("Default font"),
               new FontFamily("Arial"),
               new FontFamily("Courier New"),
               new FontFamily("Times New Roman"),
               new FontFamily("Trebuchet MS"),
               new FontFamily("Verdana")
            };
            _cmb2.ItemsSource = ff;

            // enable autosearch in both lists
            _cmb1.ItemConverter = new DataItemConverter();
            //_cmb2.ItemConverter = new FontFamilyConverter(); // standard FontFamily converter
        }

        // build a list of DataItem objects
        List<DataItem> GetItems()
        {
            List<DataItem> members = new List<DataItem>();
            foreach (MemberInfo mi in this.GetType().GetMembers())
            {
                members.Add(new DataItem(mi));
                if (members.Count == 30) break;
            }
            return members;
        }
    }


    public class DataItem
    {
        public string ItemName { get; set; }
        public MemberTypes ItemType { get; set; }
        public DataItem(MemberInfo mi)
        {
            ItemName = mi.Name;
            ItemType = mi.MemberType;
        }
        public override string ToString()
        {
            return string.Format("{0} {1}", ItemType, ItemName);
        }
    }

    public class DataItemConverter 
        : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
                return string.Empty;

            return ((DataItem)value).ItemName;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return null;
        }
    }
}
