private static ModelItem FindActivityModelItem(ModelTreeManager modelTreeManager, Activity errorTarget)
        {
            // Search the lowest Activity
            ModelItem       lowestModelItem = null;
            List <Activity> parentChain     = GetParentChain(errorTarget);

            Fx.Assert(parentChain != null, "Cannot find parent chain for " + errorTarget.DisplayName);

            foreach (Activity parent in parentChain)
            {
                lowestModelItem = modelTreeManager.GetModelItem(parent);
                if (lowestModelItem != null)
                {
                    break;
                }
            }

            ModelItem foundItem = null;

            // Find in nearest parent first.
            if (lowestModelItem != null)
            {
                // The foundItem could be null because lowestModelItem is not errorTarget's parent any more.
                // This happens if background validation hasn't finished updating errorTarget's parent.
                foundItem = ModelTreeManager.FindFirst(lowestModelItem, (modelItem) => (modelItem.GetCurrentValue() == errorTarget));
            }

            // Not found, search from root.
            if (foundItem == null)
            {
                foundItem = FindActivityModelItemFromRoot(modelTreeManager, errorTarget);
            }

            return(foundItem);
        }
            public bool TryReplaceArgument(ModelTreeManager modelTreeManager, ValidationService validationService)
            {
                Fx.Assert(modelTreeManager != null, "modelTreeManager cannot be null.");
                Fx.Assert(validationService != null, "validationService cannot be null.");

                ModelItem expressionModelItem = modelTreeManager.GetModelItem(this.ExpressionToReplace);

                if (expressionModelItem != null)
                {
                    ModelItem argumentModelItem = expressionModelItem.Parent;
                    ModelItem parentObject      = argumentModelItem.Parent;

                    if (argumentModelItem.Source != null)
                    {
                        ModelProperty argumentProperty     = parentObject.Properties[argumentModelItem.Source.Name];
                        Type          argumentPropertyType = argumentProperty.PropertyType;
                        if (argumentPropertyType == typeof(InArgument) || argumentPropertyType == typeof(OutArgument) || argumentPropertyType == typeof(InOutArgument))
                        {
                            ModelItem oldArgumentModel = argumentProperty.Value;
                            ModelItem newArgumentModel = argumentProperty.SetValue(this.NewArgument);

                            // Make sure argument.Expression is wrapped in ModelItem as well
                            ModelItem newExpressionModel = newArgumentModel.Properties["Expression"].Value;

                            return(true);
                        }
                    }
                }
                else
                {
                    Activity parentActivity = ValidationService.GetParent(this.ExpressionToReplace);
                    if (this.ArgumentAccessor.Setter != null)
                    {
                        try
                        {
                            validationService.DeactivateValidation();
                            this.ArgumentAccessor.Setter(parentActivity, this.NewArgument);
                            return(true);
                        }
                        finally
                        {
                            validationService.ActivateValidation();
                        }
                    }
                }

                return(false);
            }
            public bool TryReplaceArgument(ModelTreeManager modelTreeManager, ValidationService validationService)
            {
                Fx.Assert(modelTreeManager != null, "modelTreeManager cannot be null.");
                Fx.Assert(validationService != null, "validationService cannot be null.");

                ModelItem expressionModelItem = modelTreeManager.GetModelItem(this.ExpressionToReplace);
                if (expressionModelItem != null)
                {
                    ModelItem argumentModelItem = expressionModelItem.Parent;
                    ModelItem parentObject = argumentModelItem.Parent;

                    if (argumentModelItem.Source != null)
                    {
                        ModelProperty argumentProperty = parentObject.Properties[argumentModelItem.Source.Name];
                        Type argumentPropertyType = argumentProperty.PropertyType;
                        if (argumentPropertyType == typeof(InArgument) || argumentPropertyType == typeof(OutArgument) || argumentPropertyType == typeof(InOutArgument))
                        {
                            ModelItem oldArgumentModel = argumentProperty.Value;
                            ModelItem newArgumentModel = argumentProperty.SetValue(this.NewArgument);

                            // Make sure argument.Expression is wrapped in ModelItem as well
                            ModelItem newExpressionModel = newArgumentModel.Properties["Expression"].Value;

                            return true;
                        }
                    }
                }
                else
                {
                    Activity parentActivity = ValidationService.GetParent(this.ExpressionToReplace);
                    if (this.ArgumentAccessor.Setter != null)
                    {
                        try
                        {
                            validationService.DeactivateValidation();
                            this.ArgumentAccessor.Setter(parentActivity, this.NewArgument);
                            return true;
                        }
                        finally
                        {
                            validationService.ActivateValidation();
                        }
                    }
                }

                return false;
            }
        private static ModelItem FindActivityModelItem(ModelTreeManager modelTreeManager, Activity errorTarget)
        {
            // Search the lowest Activity
            ModelItem lowestModelItem = null;
            List<Activity> parentChain = GetParentChain(errorTarget);
            Fx.Assert(parentChain != null, "Cannot find parent chain for " + errorTarget.DisplayName);

            foreach (Activity parent in parentChain)
            {
                lowestModelItem = modelTreeManager.GetModelItem(parent);
                if (lowestModelItem != null)
                {
                    break;
                }
            }

            ModelItem foundItem = null;
            // Find in nearest parent first.
            if (lowestModelItem != null)
            {
                // The foundItem could be null because lowestModelItem is not errorTarget's parent any more.
                // This happens if background validation hasn't finished updating errorTarget's parent.
                foundItem = ModelTreeManager.FindFirst(lowestModelItem, (modelItem) => (modelItem.GetCurrentValue() == errorTarget));
            }

            // Not found, search from root.
            if (foundItem == null)
            {
                foundItem = FindActivityModelItemFromRoot(modelTreeManager, errorTarget);
            }

            return foundItem;
        }