1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
9 using System.Activities;
10 using System.Diagnostics;
13 using System.Activities.Presentation.View;
14 using System.Windows.Threading;
16 class GenericArgumentUpdater
18 EditingContext context;
19 const string TypeArgumentPropertyName = "TypeArgument";
21 public GenericArgumentUpdater(EditingContext context)
23 this.context = context;
26 public void AddSupportForUpdatingTypeArgument(Type modelItemType)
28 AttachedProperty<Type> typeArgumentProperty = new AttachedProperty<Type>
30 Name = TypeArgumentPropertyName,
31 OwnerType = modelItemType,
32 Getter = (modelItem) => modelItem.Parent == null ? null : GetTypeArgument(modelItem),
33 Setter = (modelItem, value) => UpdateTypeArgument(modelItem, value),
36 this.context.Services.GetService<AttachedPropertiesService>().AddProperty(typeArgumentProperty);
39 private static void UpdateTypeArgument(ModelItem modelItem, Type value)
43 Type oldModelItemType = modelItem.ItemType;
44 Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
45 Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value);
46 Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further");
47 ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType));
48 MorphHelper.MorphObject(modelItem, newModelItem);
49 MorphHelper.MorphProperties(modelItem, newModelItem);
51 if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity)))
53 if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal))
55 newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType));
59 DesignerView designerView = modelItem.GetEditingContext().Services.GetService<DesignerView>();
60 if (designerView != null)
62 Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() =>
64 if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem)
66 designerView.MakeRootDesigner(newModelItem, true);
68 Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem);
74 private static Type GetTypeArgument(ModelItem modelItem)
76 Fx.Assert(modelItem.ItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
77 return modelItem.ItemType.GetGenericArguments()[0];
80 private static string GetActivityDefaultName(Type activityType)
82 Fx.Assert(activityType.IsSubclassOf(typeof(Activity)), "activityType is not a subclass of System.Activities.Activity");
83 Activity activity = (Activity)Activator.CreateInstance(activityType);
84 return activity.DisplayName;