using System;
using System.Net;
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.Windows.Data;
using System.Linq;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;

namespace C1_Demo
{
    public class FontFamilyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var font = value as FontFamily;
            if (targetType == typeof(string) && font != null)
            {
                return font.ToString().Split('#').LastOrDefault();
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

    public class FontFamilyStringConverter : TypeConverter
    {
        Dictionary<object, object> _cache = new Dictionary<object, object>();

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value != null)
            {
                var name = new FontFamilyConverter().Convert(value, destinationType, null, null);
                if (name != null)
                {
                    _cache[name] = value;
                    return name;
                }
            }
            return null;
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object font;
            _cache.TryGetValue(value, out font);
            return font;
        }
    }
}
