1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing
7 using System.Diagnostics;
8 using System.Diagnostics.CodeAnalysis;
10 using System.Windows.Controls;
11 using System.Windows.Input;
14 using System.Activities.Presentation.Model;
15 using System.Activities.Presentation.PropertyEditing;
17 using System.Activities.Presentation.Internal.PropertyEditing.Editors;
18 using System.Activities.Presentation.Internal.PropertyEditing.Model;
19 using System.Activities.Presentation.Internal.PropertyEditing.Resources;
20 using System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.ValueEditors;
23 // Root WPF control used for DialogPropertyValueEditors. It contains a place for the
24 // editor and adds standard OK / Cancel buttons. It's also responsible for handling
25 // PropertyValueEditorCommands and opening and closing the root transaction that
26 // responds to the OK / Cancel buttons.
28 internal partial class PropertyValueDialogControl
31 private ModelEditingScope _rootTransaction;
32 private PropertyValueEditorCommandHandler _defaultCommandHandler;
37 // <param name="property">Property to display</param>
38 // <param name="valueDialogTemplate">Template to use</param>
39 public PropertyValueDialogControl(PropertyEntry property, DataTemplate valueDialogTemplate)
41 Fx.Assert(property != null, "property parameter is null");
42 Fx.Assert(valueDialogTemplate != null, "valueDialogTemplate parameter is null");
44 ModelPropertyEntry modelPropertyValue = property as ModelPropertyEntry;
45 if (modelPropertyValue != null)
47 _rootTransaction = modelPropertyValue.FirstModelProperty.Value.BeginEdit();
50 InitializeComponent();
52 // Make sure we use PI-specific resources within this control
53 this.Resources.MergedDictionaries.Add(PropertyInspectorResources.GetResources());
55 // Hook into an opening of nested dialogs. PropertyInspector class takes care of this for us.
56 // However, we are using Blend's collection editor which doesn't do the same thing, so
57 // we need to reproduce that behavior manually.
58 PropertyValueDialogHost.AttachOpenDialogHandlers(this);
60 // Hook into the standard set of Commands
61 _defaultCommandHandler = new PropertyValueEditorCommandHandler(this);
64 _OKButton.Click += new RoutedEventHandler(OnOkButtonClicked);
65 _cancelButton.Click += new RoutedEventHandler(OnCancelButtonClicked);
66 _contentControl.Content = property.PropertyValue;
67 _contentControl.ContentTemplate = valueDialogTemplate;
69 //Handle the commit and cancel keys within the property inspector, that is hosted in the collection editor
70 ValueEditorUtils.SetHandlesCommitKeys(this, true);
74 // Internal event we use for this control to signal that the uber-dialog (which is actually
75 // a Form) should be closed. This event is raised whenever the OK / Cancel buttons are
77 internal event EventHandler CloseParentDialog;
79 [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Propagating the error might cause VS to crash")]
80 [SuppressMessage("Reliability", "Reliability108", Justification = "Propagating the error might cause VS to crash")]
81 private void OnOkButtonClicked(object sender, RoutedEventArgs e)
85 CommitDanglingTransactions();
86 CommitRootTransaction();
87 SignalCloseParentDialog();
91 ErrorReporting.ShowErrorMessage(ex.Message);
95 private void OnCancelButtonClicked(object sender, RoutedEventArgs e)
97 AbortDanglingTransactions();
98 AbortRootTransaction();
99 SignalCloseParentDialog();
103 private void CommitDanglingTransactions()
105 _defaultCommandHandler.CommitOpenTransactions();
108 private void AbortDanglingTransactions()
110 _defaultCommandHandler.AbortOpenTransactions();
113 private void SignalCloseParentDialog()
115 if (CloseParentDialog != null)
117 CloseParentDialog(this, EventArgs.Empty);
121 // Called when the parent dialog gets closed. If this happened without us clicking on
122 // the OK or Cancel button, the root transaction will still exist and we need to manually
124 public void OnParentDialogClosing()
126 AbortRootTransaction();
129 private void CommitRootTransaction()
131 if (_rootTransaction == null)
136 _rootTransaction.Complete();
137 _rootTransaction = null;
140 private void AbortRootTransaction()
142 if (_rootTransaction == null)
147 _rootTransaction.Revert();
148 _rootTransaction = null;