1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing
7 using System.ComponentModel;
8 using System.Diagnostics;
9 using System.Diagnostics.CodeAnalysis;
10 using System.Globalization;
12 using System.Windows.Input;
13 using System.Windows.Forms;
14 using System.Windows.Forms.Integration;
17 using System.Activities.Presentation.Model;
18 using System.Activities.Presentation.PropertyEditing;
20 using System.Activities.Presentation.Internal.PropertyEditing.Model;
21 using System.Activities.Presentation.Internal.PropertyEditing.Editors;
22 using Microsoft.Win32;
25 // Helper class that is responsible for opening a [....] Form that hosts the WPF
26 // PropertyValueDialogControl that ultimately hosts a DialogPropertyValueEditor.
27 // Both PropertyInspector control as well as PropertyValueDialogControl use this class.
29 internal class PropertyValueDialogHost
32 private System.Windows.Forms.Form _dialogWindow;
34 // Ctor is private because it is created automatically when the OpenDialogWindow fires.
35 private PropertyValueDialogHost()
40 // Helper that attaches the OpenDialogWindow command to a handler within this host
41 // class. The handler will automatically open and populate the dialog window when
42 // the OpenDialogWindow command is fired.
44 // <param name="dialogRoot">UIElement that should handle the OpenDialogWindow command</param>
45 public static void AttachOpenDialogHandlers(UIElement dialogRoot)
47 Fx.Assert(dialogRoot != null, "dialogRoot parameter is null");
49 PropertyValueDialogHost dialogHost = new PropertyValueDialogHost();
50 dialogRoot.CommandBindings.Add(new CommandBinding(PropertyContainer.OpenDialogWindow, dialogHost.OnOpenDialogWindow, dialogHost.OnCanOpenDialogWindow));
54 // Called in response to OpenDialogWindow command firing. A dialog can be opened if
55 // a dialog editor DataTemplate exists on the corresponding property being edited.
57 // <param name="sender"></param>
58 // <param name="e"></param>
59 public void OnCanOpenDialogWindow(object sender, CanExecuteRoutedEventArgs e)
62 DataTemplate dialogEditorTemplate = GetDialogEditorTemplate(GetParentProperty(e.OriginalSource));
63 if (dialogEditorTemplate == null)
68 // We can show a dialog
73 // Called in response to OpenDialogWindow command firing. The method opens a
74 // [....] Form that contains an ElementHost that, in turn, contains
75 // PropertyValueDialogControl.
77 // <param name="sender"></param>
78 // <param name="e"></param>
79 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
80 public void OnOpenDialogWindow(object sender, ExecutedRoutedEventArgs e)
82 // Hook on to the system user-preference changed event.
83 SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
85 // Is there a DataTemplate and PropertyValue to display?
86 PropertyEntry parentProperty = GetParentProperty(e.OriginalSource);
87 DataTemplate dialogEditorTemplate = GetDialogEditorTemplate(parentProperty);
88 if (dialogEditorTemplate == null)
93 // Create and populate a new Form
94 _dialogWindow = new Form();
95 _dialogWindow.ShowInTaskbar = false;
96 _dialogWindow.ShowIcon = false;
97 _dialogWindow.MaximizeBox = false;
98 _dialogWindow.MinimizeBox = false;
99 _dialogWindow.HelpButton = false;
100 _dialogWindow.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
101 _dialogWindow.Width = 600;
102 _dialogWindow.Height = 400;
104 // We need to change the title based on the type of the property being edited.
105 // e.g: For CollectionEditors we should say "CollectionEditor : <DisplayName>"
106 // For everything else we should say "Property Editor : <DisplayName>"
107 string title = System.Activities.Presentation.Internal.Properties.Resources.PropertyEditing_DialogValueEditorTitle;
108 _dialogWindow.MinimumSize = new System.Drawing.Size(575, 400); // Magic min-size numbers from Dan
109 _dialogWindow.Text = string.Format(
110 CultureInfo.CurrentCulture,
112 parentProperty.DisplayName);
114 PropertyValueDialogControl dialogControl = new PropertyValueDialogControl(parentProperty, dialogEditorTemplate);
115 dialogControl.CloseParentDialog += new EventHandler(OnCloseParentDialog);
117 using (ElementHost elementHost = new ElementHost())
119 elementHost.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
120 elementHost.Size = _dialogWindow.ClientSize;
121 elementHost.Child = dialogControl;
123 _dialogWindow.Controls.Add(elementHost);
124 _dialogWindow.ShowDialog();
125 dialogControl.OnParentDialogClosing();
130 // Event handler for system color changes. This will change the BackColor of the dialog
131 // matching the current tool window background color for Visual Studio.
133 // <param name="sender"></param>
134 // <param name="e"></param>
136 private void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
139 // if (PropertyInspectorFontAndColorDictionary.ColorService != null) {
140 // System.Drawing.Color backColor = PropertyInspectorFontAndColorDictionary.ColorService.GetColor(Microsoft.VisualStudio.Shell.Interop.__VSSYSCOLOREX.VSCOLOR_TOOLWINDOW_BACKGROUND, 255);
141 // _dialogWindow.BackColor = backColor;
142 // _dialogWindow.Refresh();
143 // _dialogWindow.Invalidate(true);
147 // Event handler invoked when the PropertyValueDialogControl indicates that the dialog should close
148 private void OnCloseParentDialog(object sender, EventArgs e)
150 // Unhook on to the system user-preference changed event.
151 SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
153 if (_dialogWindow != null)
155 _dialogWindow.Close();
159 private static DataTemplate GetDialogEditorTemplate(PropertyEntry property)
161 if (property == null)
166 // Does the current PropertyEntry have a dialog editor?
167 DialogPropertyValueEditor dialogEditor = property.PropertyValueEditor as DialogPropertyValueEditor;
168 if (dialogEditor == null)
173 return dialogEditor.DialogEditorTemplate;
176 private static PropertyEntry GetParentProperty(object showDialogCommandSource)
178 // Was the command invoked from a control that we expect?
179 DependencyObject source = showDialogCommandSource as DependencyObject;
185 PropertyContainer container = PropertyContainer.GetOwningPropertyContainer(source);
186 if (container == null)
191 // Does the current PropertyEntry have a dialog editor?
192 return container.PropertyEntry;