Exemplo n.º 1
0
        private object AttemptConvert(BindingDef binding, object value, IBindingTarget container)
        {
            if (binding.HasValueConverter)
            {
                IValueConverter converter = binding.GetValueConverterInstance();
                value = converter.Convert(value, binding.ResolveAsTarget(container).Descriptor.PropertyType, binding, CultureInfo.CurrentCulture);
            }

            return(value);
        }
Exemplo n.º 2
0
        //TODO: Should probably be a member of binding.
        private void SetProperty(BindingDef binding, object value)
        {
            SourceProperty property = binding.SourceExpression.ResolveAsSource(this.DataContext);

            if (property != null && property.Descriptor != null && !property.Descriptor.IsReadOnly)
            {
                object convertedValue  = null;
                Type   destinationType = property.Descriptor.PropertyType;

                if (binding.HasValueConverter)
                {
                    IValueConverter valueConverter = binding.GetValueConverterInstance();

                    convertedValue = valueConverter.ConvertBack(value, property.Descriptor.PropertyType, null, null);
                }
                else
                {
                    if (TypeHelper.IsSimpleType(value.GetType()) && TypeHelper.IsSimpleType(destinationType))
                    {
                        convertedValue = Convert.ChangeType(value, destinationType);
                    }

                    else
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
                        if (converter != null)
                        {
                            convertedValue = converter.ConvertFrom(value);
                        }
                        else
                        {
                            converter = TypeDescriptor.GetConverter(value.GetType());

                            if (converter != null)
                            {
                                convertedValue = converter.ConvertTo(value, destinationType);
                            }
                        }
                    }
                }

                property.Descriptor.SetValue(property.OwningInstance, convertedValue);
            }
        }