1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
7 using System.Activities.Presentation.Internal.PropertyEditing;
8 using System.Activities.Presentation.Model;
9 using System.Activities.Presentation.PropertyEditing;
10 using System.Activities.Presentation.Converters;
11 using System.Globalization;
13 using System.Windows.Controls;
14 using System.Windows.Data;
17 class ExpressionValueEditor : DialogPropertyValueEditor
19 public ExpressionValueEditor()
21 //default template for inline editor
22 this.InlineEditorTemplate = EditorResources.GetResources()["inlineExpressionEditorTemplate"] as DataTemplate;
25 public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
27 //get the property entry to model item converter
28 IValueConverter converter = (ModelPropertyEntryToOwnerActivityConverter)EditorResources.GetResources()["ModelPropertyEntryToOwnerActivityConverter"];
29 ModelItem item = (ModelItem)converter.Convert(propertyValue.ParentProperty, typeof(ModelItem), false, null);
30 //we need editing context
31 EditingContext ctx = ((IModelTreeItem)item).ModelTreeManager.Context;
32 //get the default dialog owner
33 DependencyObject owner = ctx.Services.GetService<DesignerView>();
35 //create and show dialog with owner, edited expression and context
36 (new EditorDialog(owner, propertyValue, ctx, this.DialogTemplate, this.DialogTitle)).ShowOkCancel();
39 protected virtual DataTemplate DialogTemplate
41 get { return (DataTemplate)EditorResources.GetResources()["dialogExpressionEditorTemplate"]; }
44 protected virtual string DialogTitle
46 get { return (string)EditorResources.GetResources()["dialogExpressionEditorTitle"]; }
49 private sealed class EditorDialog : WorkflowElementDialog
51 public EditorDialog(DependencyObject owner, PropertyValue propertyValue, EditingContext context, DataTemplate dialogTemplate, string title)
56 this.WindowResizeMode = ResizeMode.CanResize;
57 this.WindowSizeToContent = SizeToContent.Manual;
60 this.Context = context;
62 ContentPresenter contentPresenter = new ContentPresenter()
64 Content = propertyValue,
65 //get default editor template for content presenter
66 ContentTemplate = dialogTemplate
69 this.Content = contentPresenter;
70 this.Loaded += OnWindowLoaded;
73 void OnWindowLoaded(object sender, RoutedEventArgs args)
75 ContentPresenter presenter = (ContentPresenter)this.Content;
76 PropertyValue propertyValue = (PropertyValue)presenter.Content;
77 Button okButton = (Button)this.FindName("okButton");
78 ExpressionTextBox etb = VisualTreeUtils.GetNamedChild<ExpressionTextBox>(presenter, "PART_expressionTextBox");
79 TextBlock hint = VisualTreeUtils.GetNamedChild<TextBlock>(presenter, "PART_hintText");
80 Fx.Assert(etb != null, "ExpressionTextBox with name 'PART_expressionTextBox' should be in the template!");
81 Fx.Assert(hint != null, "Hint TextBlock with name 'PART_hintText' should be in the template!");
82 //bind button with ETB's commit command
83 okButton.Command = DesignerView.CommitCommand;
84 okButton.CommandTarget = etb;
85 etb.Loaded += new RoutedEventHandler(OnExpressionTextBoxLoaded);
87 if (null != etb && null != hint)
89 IValueConverter typeToStringConverter = (IValueConverter)EditorResources.GetResources()["TypeParameterConverter"];
90 string hintFormatString = (string)EditorResources.GetResources()["dialogExpressionEditorHintFormatString"];
92 //convert expression's container type to friendly name (i.e. replace generic '1 with <T>)
93 string friendlyTypeName = (string)
94 typeToStringConverter.Convert(etb.ExpressionType ?? propertyValue.ParentProperty.PropertyType, typeof(string), null, CultureInfo.CurrentCulture);
96 //format editor title to include friendly type name and property name
97 hint.Text = string.Format(CultureInfo.CurrentCulture, hintFormatString, propertyValue.ParentProperty.PropertyName, friendlyTypeName);
101 void OnExpressionTextBoxLoaded(object sender, RoutedEventArgs e)
103 (sender as ExpressionTextBox).BeginEdit();