Пример #1
0
        // Value is a string, we need to create a native object.
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            System.Diagnostics.Debug.WriteLine("Calling ConvertBack from PropDirectConverter.");

            DirectConverterParameter dcp = (DirectConverterParameter)parameter;
            TwoTypes tt = dcp.GetTwoTypes();

            if (value == null)
            {
                return(Binding.DoNothing);
            }

            try
            {
                object pValue = ((IPropBagMin)value).GetValWithType(dcp.PropertyName, dcp.SourceType);
                object result = ValueConverter.ConvertBack(pValue, targetType, tt, culture);

                // Set the value, here, since this converter expects an entire IPropBagMin.
                ((IPropBagMin)value).SetValWithType(dcp.PropertyName, dcp.SourceType, pValue);
            }
            catch
            {
                // Suppress all errors.
            }
            return(Binding.DoNothing);
        }
Пример #2
0
        // Value is native object, we need to return a targetType (hopefully a string at this point.)
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(string))
            {
                if (value == null)
                {
                    return(string.Empty);
                }
            }

            //System.Diagnostics.Debug.Assert(value == null || targetType.IsAssignableFrom(typeof(string)), $"PropFactory expected target type to be string, but was type: {targetType}.");

            // The parameter, if only specifying one type, is specifying the type
            // of the native (i.e., source) object.
            TwoTypes tt = FromMkUpExtParam(parameter, typeof(string));

            if (tt.IsEmpty)
            {
                throw new InvalidOperationException("Type information was not available.");
            }
            else if (targetType == tt.SourceType /* || targetType == typeof(object)*/)
            {
                return(value);
            }
            if (tt.DestType == typeof(object))
            {
                return(value);
            }

            Type vType = value.GetType();

            if (vType == tt.DestType || tt.DestType.IsAssignableFrom(vType))
            {
                System.Diagnostics.Debug.WriteLine($"The Destination Type specified in the binding parameter is assignable from the run-time type of the value being converted: {vType}.");
                System.Diagnostics.Debug.Assert(tt.DestType.IsAssignableFrom(value.GetType()), $"DestinationType: { tt.DestType } is not assignable from the run-time type of the value to be converted: {value.GetType()}.");
                return(value);
            }
            else if (tt.DestType != typeof(string))
            {
                throw new NotImplementedException("Converting to a type other than a string is not supported.");
            }
            else
            {
                StringFromTDelegate del = _converter.GetTheStringFromTDelegate(tt.SourceType, tt.DestType);
                return(del(value));
            }
        }
Пример #3
0
        // Value is native object, we need to return a targetType (hopefully a string at this point.)
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            System.Diagnostics.Debug.WriteLine("Calling Convert from PropDirectConverter.");

            DirectConverterParameter dcp = (DirectConverterParameter)parameter;
            TwoTypes tt = dcp.GetTwoTypes();

            object pValue = ((IPropBagMin)value).GetValWithType(dcp.PropertyName, dcp.SourceType);

            try
            {
                object result = ValueConverter.Convert(pValue, targetType, tt, culture);
                return(result);
            }
            catch
            {
                return(Binding.DoNothing);
            }
        }
Пример #4
0
        // Value is a string, we need to create a native object.
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
#if DEBUG
            TwoTypes tt = FromMkUpExtParam(parameter, typeof(string));
            if (tt.IsEmpty)
            {
                throw new InvalidOperationException("Type information was not available.");
            }

            // Check to see if the specified type is compatible with the type of the property the converter is asking for.
            if (!targetType.IsAssignableFrom(tt.SourceType))
            {
                System.Diagnostics.Debug.WriteLine("The SourceType specified by the binding is not compatible with the TargetType on ConvertBack (PropFactoryValueConverter.");
            }

            if (value == null && !targetType.IsValueType)
            {
                return(null);
            }
            //System.Diagnostics.Debug.Assert(value == null || typeof(string).IsAssignableFrom(value.GetType()), $"PropFactory expected string input on convert back, but type was {value.GetType()}.");

            //if (targetType == typeof(object)) return value;
#else
            if (value == null && !targetType.IsValueType)
            {
                return(null);
            }
            if (targetType == typeof(object))
            {
                return(value);
            }

            //System.Diagnostics.Debug.Assert(value == null || typeof(string).IsAssignableFrom(value.GetType()), $"PropFactory expected string input on convert back, but type was {value.GetType()}.");

            TwoTypes tt = TwoTypes.FromMkUpExtParam(parameter, typeof(string));
            if (tt.IsEmpty)
            {
                throw new InvalidOperationException("Type information was not available.");
            }
#endif
            if (value == null)
            {
                if (targetType.IsValueType)
                {
                    return(GetDefaultValue(targetType, "ConvertBackOp"));
                }
            }
            else
            {
                Type sourceRunTimeType = value?.GetType();

                if (sourceRunTimeType != tt.SourceType)
                {
                    if (sourceRunTimeType != typeof(string))
                    {
                        throw new NotImplementedException("Converting from a type other than a string is not yet supported.");
                    }

                    TFromStringDelegate del = _converter.GetTheTFromStringDelegate(tt.SourceType, tt.DestType);
                    string s = value as string;
                    return(del(s));
                }
            }

            return(value);
        }