ViewModelToViewConverter

Topics: Feature requests
Aug 29, 2012 at 1:14 PM

Hi Geert,

how about adding the following ViewModelToViewModelConverter? I use it a lot in conjunction with ContentControls to glue ViewModels and Views together... (for example to create an office style options dialog ....)

    public class ViewModelToViewConverter : MarkupExtension, IValueConverter
    {
        #region Public Methods and Operators

         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (Environment.IsInDesignMode || (value == null))
            {
                return ConverterHelper.DoNothingBindingValue;
            }

            var viewLocator = ServiceLocator.Instance.GetInstance<IViewLocator>();
            var viewType = viewLocator.ResolveView(value.GetType());
            return (viewType != null) ? ViewHelper.ConstructViewWithViewModel(viewType, value) : ConverterHelper.DoNothingBindingValue;
        }

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

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        #endregion
    }

 

As you can see the converter is not derived from ValueConverterBase but from MarkupExtension because i want to use this converter without instantiating it in the resources. It would be helpful if you could derive ValueConverterBase from MarkupExtension to allow a direct use in the bindings ...

Usage:

<ContentControl Content="{Binding Path=ViewModel, Mode=OneWay, Converter={catel:ViewModelToViewConverter}}" />

Thanks a lot!

Coordinator
Aug 30, 2012 at 4:42 PM

Excellent addition, thank you very much! I just added this to the source code, will release a new build via NuGet tonight.

Aug 31, 2012 at 5:08 PM

I'm glad that i can contribute something to this great framework.....

Could you also please add the following extension to ValueConverterBase to allow direct use in the binding of all catel converters (without instantiating them first in the resources)? Many thanks!

	public abstract class ValueConverterBase : 
                // ---------------
#if
SL5 || WPF MarkupExtension, #endif                 // ---------------
IValueConverter { // --------------- public override object ProvideValue(IServiceProvider serviceProvider) { return this; } // --------------- protected CultureInfo CurrentCulture { get; private set; } public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) { this.CurrentCulture = culture; return this.Convert(value, targetType, parameter); } public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { this.CurrentCulture = culture; return this.ConvertBack(value, targetType, parameter); } protected abstract object Convert(object value, Type targetType, object parameter); protected virtual object ConvertBack(object value, Type targetType, object parameter) { return ConverterHelper.DoNothingBindingValue; } }
Coordinator
Aug 31, 2012 at 8:49 PM

Done! Also updated the ViewModelToViewConverter so it uses ValueConverterBase.

Sep 2, 2012 at 6:06 PM

Thanks a lot for this fast update!