Beispiel #1
0
        public static object ConvertToType(object value, Type type)
        {
            if (value == null)
            {
                return(null);
            }
            if (type.IsAssignableFrom(value.GetType()))
            {
                return(value);
            }
            TypeConverter typeConverter = ConverterHelper.GetTypeConverter(type);

            if (typeConverter != null && typeConverter.CanConvertFrom(value.GetType()))
            {
                value = typeConverter.ConvertFrom(value);
                return(value);
            }
            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// Sets the property
        /// </summary>
        /// <param name="parameter">Unused.</param>
        protected override void Invoke(object parameter)
        {
            if (!string.IsNullOrEmpty(this.PropertyName) && (this.Target != null))
            {
                Type         c        = this.Target.GetType();
                PropertyInfo property = c.GetProperty(this.PropertyName);
                if (property == null)
                {
                    throw new ArgumentException("Cannot find property " + this.PropertyName + " on object " + this.Target.GetType().Name);
                }

                if (!property.CanWrite)
                {
                    throw new ArgumentException("Property is read-only " + this.PropertyName + " on object " + this.Target.GetType().Name);
                }

                object        toValue        = this.Value;
                TypeConverter typeConverter  = ConverterHelper.GetTypeConverter(property.PropertyType);
                Exception     innerException = null;
                try
                {
                    if (((typeConverter != null) && (this.Value != null)) && typeConverter.CanConvertFrom(this.Value.GetType()))
                    {
                        toValue = typeConverter.ConvertFrom(this.Value);
                    }

                    object fromValue = property.GetValue(this.Target, null);

                    if (this.Increment)
                    {
                        toValue = SetProperty.Add(toValue, fromValue);
                    }

                    if (this.Duration.HasTimeSpan)
                    {
                        Timeline timeline;
                        if ((typeof(FrameworkElement).IsAssignableFrom(c) && ((this.PropertyName == "Width") || (this.PropertyName == "Height"))) && double.IsNaN((double)fromValue))
                        {
                            FrameworkElement target = (FrameworkElement)this.Target;
                            if (this.PropertyName == "Width")
                            {
                                fromValue = target.ActualWidth;
                            }
                            else
                            {
                                fromValue = target.ActualHeight;
                            }
                        }

                        Storyboard storyboard = new Storyboard();
                        if (typeof(double).IsAssignableFrom(property.PropertyType))
                        {
                            DoubleAnimation animation = new DoubleAnimation();
                            animation.From           = new double?((double)fromValue);
                            animation.To             = new double?((double)toValue);
                            animation.EasingFunction = this.Ease;
                            timeline = animation;
                        }
                        else if (typeof(Color).IsAssignableFrom(property.PropertyType))
                        {
                            ColorAnimation animation2 = new ColorAnimation();
                            animation2.From           = new Color?((Color)fromValue);
                            animation2.To             = new Color?((Color)toValue);
                            animation2.EasingFunction = this.Ease;
                            timeline = animation2;
                        }
                        else if (typeof(Point).IsAssignableFrom(property.PropertyType))
                        {
                            PointAnimation animation3 = new PointAnimation();
                            animation3.From           = new Point?((Point)fromValue);
                            animation3.To             = new Point?((Point)toValue);
                            animation3.EasingFunction = this.Ease;
                            timeline = animation3;
                        }
                        else
                        {
                            ObjectAnimationUsingKeyFrames frames = new ObjectAnimationUsingKeyFrames();
                            DiscreteObjectKeyFrame        frame  = new DiscreteObjectKeyFrame();
                            frame.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0L));
                            frame.Value   = fromValue;
                            DiscreteObjectKeyFrame frame2 = new DiscreteObjectKeyFrame();
                            frame2.KeyTime = KeyTime.FromTimeSpan(this.Duration.TimeSpan);
                            frame2.Value   = toValue;
                            frames.KeyFrames.Add(frame);
                            frames.KeyFrames.Add(frame2);
                            timeline = frames;
                        }
                        timeline.Duration = this.Duration;
                        storyboard.Children.Add(timeline);
                        Storyboard.SetTarget(storyboard, this.Target);
                        Storyboard.SetTargetProperty(storyboard, new PropertyPath(property.Name, new object[0]));
                        storyboard.Begin();
                    }
                    else
                    {
                        property.SetValue(this.Target, toValue, new object[0]);
                    }
                }
                catch (FormatException exception2)
                {
                    innerException = exception2;
                }
                catch (ArgumentException exception3)
                {
                    innerException = exception3;
                }
                catch (MethodAccessException exception4)
                {
                    innerException = exception4;
                }

                if (innerException != null)
                {
                    throw new ArgumentException(innerException.Message);
                }
            }
        }