1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.Converters
7 using System.Diagnostics;
8 using System.Globalization;
9 using System.Windows.Data;
10 using System.Activities.Presentation.Model;
11 using System.Activities.Presentation.Internal.PropertyEditing.Model;
13 // This converter converts from Argument to Argument.Expression ModelItem using the ModelPropertyEntry instance.
14 // on the reverse direction it creates a new Argument and sets the expression on it with the value
15 // The direction of the created argument is provided by the converter parameter.
16 //1st binding is to the PropertyValue
17 //2nd binding is to the ModelProprtyEntry (to get the modelItem).
18 public class ArgumentToExpressionModelItemConverter : IMultiValueConverter
20 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
22 ModelItem convertedValue = null;
23 if (values[1] != null)
25 ModelPropertyEntry modelPropertyEntry = values[1] as ModelPropertyEntry;
26 if (modelPropertyEntry != null)
28 ModelProperty property = modelPropertyEntry.FirstModelProperty;
31 ModelItem argumentModelItem = property.Value;
32 if (argumentModelItem != null &&
33 argumentModelItem.Properties["Expression"] != null &&
34 argumentModelItem.Properties["Expression"].Value != null)
36 convertedValue = argumentModelItem.Properties["Expression"].Value;
41 return convertedValue;
44 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
46 Argument target = null;
47 ArgumentDirection direction = ArgumentDirection.In;
48 string directionString = parameter as string;
49 if (!string.IsNullOrEmpty(directionString))
51 direction = (ArgumentDirection)Enum.Parse(typeof(ArgumentDirection), directionString);
54 Activity expression = null;
55 ModelItem valueExpressionModelItem = value as ModelItem;
56 if (valueExpressionModelItem != null && typeof(Activity).IsAssignableFrom(valueExpressionModelItem.ItemType))
58 expression = (Activity)valueExpressionModelItem.GetCurrentValue();
60 ActivityWithResult activityWithResult = expression as ActivityWithResult;
61 if (expression != null && activityWithResult != null)
63 // In the In case the expression is of type Activity<T> so we want to create InArgument<T>
64 // In Out and InOut case the expresion is Activity<Location<T>> we want to create OutArgument<T>,InOutArgument<T>
66 if (direction == ArgumentDirection.In)
68 argumentType = activityWithResult.ResultType;
72 // if expression type is Location<T> argument type is T
73 argumentType = activityWithResult.ResultType.GetGenericArguments()[0];
75 target = Argument.Create(argumentType, direction);
76 target.Expression = activityWithResult;
78 return new object[] { target };