Esempio n. 1
0
        object ConvertFromTargetToSource(object value)
        {
            var sourceType = Property.PropertyType;
            var targetType = PropertyPathWalker.FinalNode.ValueType;

            if (targetType.IsAssignableFrom(sourceType))
            {
                return(value);
            }

            // Note, we only call the ConvertTo/ConvertFrom methods if the TypeConverter supports *both
            // the forward conversion and reverse conversion. A type converter which returns 'false' for
            // either CanConvertTo or CanConvertFrom is not used.
            var converter = Helper.GetConverterFor(sourceType);

            if (converter != null && converter.CanConvertFrom(targetType) && converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(null, GetConverterCulture(), value, targetType));
            }

            converter = Helper.GetConverterFor(targetType);
            if (converter != null && converter.CanConvertFrom(sourceType) && converter.CanConvertTo(sourceType))
            {
                return(converter.ConvertFrom(null, GetConverterCulture(), value));
            }

            return(MoonlightTypeConverter.ConvertObject(PropertyPathWalker.FinalNode.PropertyInfo, value, Target.GetType()));
        }
Esempio n. 2
0
        internal override object GetValue(DependencyProperty dp)
        {
            var    source = Target.TemplateOwner;
            object value  = null;

            if (source != null)
            {
                value = source.GetValue(SourceProperty);
            }
            return(MoonlightTypeConverter.ConvertObject(TargetProperty, value, Target.GetType(), false));
        }
Esempio n. 3
0
        object ConvertToType(DependencyProperty dp, object value)
        {
            try {
                if (!PropertyPathWalker.IsPathBroken && Binding.Converter != null)
                {
                    value = Binding.Converter.Convert(
                        value,
                        Property.PropertyType,
                        Binding.ConverterParameter,
                        GetConverterCulture()
                        );
                }

                if (value == DependencyProperty.UnsetValue || PropertyPathWalker.IsPathBroken)
                {
                    value = Binding.FallbackValue ?? dp.GetDefaultValue(Target);
                }
                else if (value == null)
                {
                    value = Binding.TargetNullValue;
                    if (value == null && IsBoundToAnyDataContext && string.IsNullOrEmpty(Binding.Path.Path))
                    {
                        value = dp.GetDefaultValue(Target);
                    }
                }
                else
                {
                    string format = Binding.StringFormat;
                    if (!string.IsNullOrEmpty(format))
                    {
                        if (!format.Contains("{0"))
                        {
                            format = "{0:" + format + "}";
                        }
                        value = string.Format(GetConverterCulture(), format, value);
                    }
                }

                if (value != null)
                {
                    value = ConvertFromSourceToTarget(value);
                }
            } catch (Exception ex) {
                return(MoonlightTypeConverter.ConvertObject(dp, Binding.FallbackValue, Target.GetType(), true));
            }
            return(value);
        }
Esempio n. 4
0
        void convert_keyframe_value_cb(Kind kind, IntPtr property_ptr, IntPtr original, out Value converted)
        {
            Type type = Deployment.Current.Types.KindToType(kind);

            if (type != null)
            {
                Types.Ensure(type);
            }

            DependencyProperty property = DependencyProperty.Lookup(property_ptr);

            if (property == null)
            {
                Console.WriteLine("Moonlight Error: Property couldn't be looked up");
                converted = Value.Empty;
                return;
            }

            object o = Value.ToObject(null, original);

            if (o == null)
            {
                Console.WriteLine("Moonlight Error: Object was null");
                converted = Value.Empty;
                return;
            }

            o = MoonlightTypeConverter.ConvertObject(property, o, null, true);

            if (o == null)
            {
                Console.WriteLine("Moonlight Error: Converted to null");
                converted = Value.Empty;
                return;
            }

            // This is freed in native code
            converted = Value.FromObject(o);
        }
Esempio n. 5
0
        private void ConvertSetterValue(Setter s)
        {
            DependencyProperty dp  = s.Property as DependencyProperty;
            object             val = s.Value;

            if (dp.PropertyType == typeof(string))
            {
                if (val == null)
                {
                    throw new ArgumentException("foo1");
                }
                else if (val.GetType() != typeof(string))
                {
                    throw new XamlParseException("foo2");
                }
            }

            try {
                s.ConvertedValue = MoonlightTypeConverter.ConvertObject(dp, val, TargetType, true);
            } catch (Exception ex) {
                throw new XamlParseException(ex.Message);
            }
        }
Esempio n. 6
0
        internal void UpdateSourceObject(object value, bool force)
        {
            // We can only update two way bindings.
            if (Binding.Mode != BindingMode.TwoWay)
            {
                return;
            }

            string    dataError   = null;
            Exception exception   = null;
            bool      oldUpdating = Updating;
            var       node        = PropertyPathWalker.FinalNode;

            try {
                // If the user calls BindingExpresion.UpdateSource (), we must update regardless of focus state.
                // Otherwise we only update if the textbox is unfocused.
                if (!force && TwoWayTextBoxText && System.Windows.Input.FocusManager.GetFocusedElement() == Target)
                {
                    return;
                }

                if (PropertyPathWalker.IsPathBroken)
                {
                    return;
                }

                if (Binding.TargetNullValue != null)
                {
                    try {
                        var v = MoonlightTypeConverter.ConvertObject(Property, Binding.TargetNullValue, Target.GetType(), true);
                        if (Helper.AreEqual(v, value))
                        {
                            value = null;
                        }
                    } catch {
                        // FIXME: I'm fairly sure we ignore this, but we need a test to be 100% sure.
                    }
                }

                if (Binding.Converter != null)
                {
                    value = Binding.Converter.ConvertBack(value,
                                                          node.ValueType,
                                                          Binding.ConverterParameter,
                                                          GetConverterCulture());
                }
                if (value is string)
                {
                    TryUseParseMethod((string)value, node.ValueType, ref value);
                }

                try {
                    if (value != null)
                    {
                        value = ConvertFromTargetToSource(value);
                    }
                } catch {
                    return;
                }

                if (cachedValue == null)
                {
                    if (value == null)
                    {
                        return;
                    }
                }

                Updating = true;
                node.SetValue(value);
                cachedValue = value;
            } catch (Exception ex) {
                if (Binding.ValidatesOnExceptions)
                {
                    if (ex is TargetInvocationException)
                    {
                        ex = ex.InnerException;
                    }
                    exception = ex;
                }
            }
            finally {
                Updating = oldUpdating;
                if (Binding.ValidatesOnDataErrors && exception == null && node.Source is IDataErrorInfo && node.PropertyInfo != null)
                {
                    dataError = ((IDataErrorInfo)node.Source) [node.PropertyInfo.Name];
                }
            }

            MaybeEmitError(dataError, exception);
        }