Ejemplo n.º 1
0
        /// <summary>
        /// Modifies the public fields values, of a target instance, with the values of another instance of the same type.
        /// Allows to indicate (true) whether the values must be compared in order to apply the change if them are different,
        /// else (false) the change will be forced for all.
        /// Returns whether any change -forced or not- was applied.
        /// </summary>
        public static bool AlterInstanceFields <TType>(ref TType Target, TType Source, bool ComparePriorToChange)
        {
            bool   ChangeWasApplied = false;
            object TargetValue, SourceValue;

            if (ComparePriorToChange)
            {
                foreach (FieldInfo FieldData in typeof(TType).GetFields())
                {
                    TargetValue = FieldData.GetValue(Target);
                    SourceValue = FieldData.GetValue(Source);

                    if ((TargetValue != null && SourceValue != null && !TargetValue.Equals(SourceValue)) ||
                        (TargetValue == null && SourceValue != null) || (TargetValue != null && SourceValue == null))
                    {
                        FieldData.SetValue(Target, SourceValue);
                        ChangeWasApplied = true;
                    }
                }
            }
            else
            {
                foreach (FieldInfo FieldData in typeof(TType).GetFields())
                {
                    FieldData.SetValue(Target, FieldData.GetValue(Source));
                }

                ChangeWasApplied = true;
            }

            return(ChangeWasApplied);
        }
Ejemplo n.º 2
0
        public static void BeginAnimation(IAnimatable Item, DependencyProperty dp, AnimationTimeline ATL, object FromValue, object ToValue, Action OnCompleted, SetExtentAnimationTimelineDelegate SetExtentValue)
        {
            if (ToValue == null)
            {
                return;
            }

            if (Item != null && Item is DependencyObject)
            {
                ATL.FillBehavior = FillBehavior.HoldEnd;

                if (SetExtentValue != null)
                {
                    SetExtentValue(ATL);
                }

                FillBehavior StatusValue = ATL.FillBehavior;
                ATL.FillBehavior = FillBehavior.Stop;

                DependencyObject ConvertItem = Item as DependencyObject;
                object           TargetValue = null;
                if (StatusValue == FillBehavior.Stop || ATL.AutoReverse)
                {
                    if (FromValue == null)
                    {
                        TargetValue = ConvertItem.GetValue(dp);
                    }
                    else
                    {
                        TargetValue = FromValue;
                    }
                }
                else
                {
                    TargetValue = ToValue;
                }

                ATL.Completed += new EventHandler(
                    delegate(object sender, EventArgs e)
                {
                    ConvertItem.SetValue(dp, TargetValue);
                    object CurValue = ConvertItem.GetValue(dp);
                    if (TargetValue != null && !TargetValue.Equals(CurValue))
                    {
                        return;
                    }

                    if (OnCompleted != null)
                    {
                        OnCompleted();
                    }
                });
                Item.BeginAnimation(dp, ATL, HandoffBehavior.SnapshotAndReplace);
            }
        }
Ejemplo n.º 3
0
            protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            {
                // get a reference to the property this validation depends upon
                var containerType = validationContext.ObjectInstance.GetType();
                var field         = containerType.GetProperty(DependentProperty);


                if (field != null)
                {
                    // get the value of the dependent property
                    var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
                    // trim spaces of dependent value
                    if (dependentValue != null && dependentValue is string)
                    {
                        dependentValue = (dependentValue as string).Trim();

                        if (!AllowEmptyStrings && (dependentValue as string).Length == 0)
                        {
                            dependentValue = null;
                        }
                    }

                    // compare the value against the target value
                    if ((dependentValue == null && TargetValue == null) ||
                        (dependentValue != null && (TargetValue.Equals("*") || dependentValue.Equals(TargetValue))))
                    {
                        // match => means we should try validating this field
                        if (!_innerAttribute.IsValid(value))
                        {
                            // validation failed - return an error
                            return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName }));
                        }
                    }
                }

                return(ValidationResult.Success);
            }