using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Reflection;

namespace C1DataGrid_Demo2010
{
    public class ImageSourceConverter : IValueConverter
    {
        #region IValueConverter Members

        private string baseUri = "/" + new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name + ";component/Resources/";

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType.FullName == "System.Windows.Media.ImageSource")
            {
                var parameterString = parameter as string;
                if (!string.IsNullOrWhiteSpace(parameterString))
                {
                    baseUri = parameterString;
                }
                BitmapImage bitmap = new BitmapImage(new Uri(baseUri + (string)value, UriKind.RelativeOrAbsolute));
                return bitmap;

            }
            return null;
        }

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

        #endregion
    }
}
