public override bool Commit(bool isExplicitCommit)
        {
            bool committed = false;

            //only generate and validate the expression when when we don't require explicit commit change
            //or when the commit is explicit
            if (!ExplicitCommit || isExplicitCommit)
            {
                // Generate and validate the expression.
                // Get the text from the snapshot and set it to the Text property
                PreviousText = null;

                if (ExpressionEditorInstance != null)
                {
                    PreviousText = Text;
                    Text         = ExpressionEditorInstance.GetCommittedText();
                }
                if (Expression != null)
                {
                    Activity expression = Expression.GetCurrentValue() as Activity;
                    // if expression is null, GetExpressionString will return null
                    PreviousText = ExpressionHelper.GetExpressionString(expression, OwnerActivity);
                }
                else
                {
                    PreviousText = null;
                }

                if (EditingTextBox != null)
                {
                    EditingTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                }

                // If the Text is null, or equal to the previous value, or changed from null to empty, don't bother generating the expression
                // We still need to generate the expression when it is changed from other value to EMPTY however - otherwise
                // the case where you had an expression (valid or invalid), then deleted the whole thing will not be evaluated.
                if (ShouldGenerateExpression(PreviousText, Text))
                {
                    GenerateExpression();
                    committed = true;
                }
            }
            if (!ContentTemplate.Equals((DataTemplate)FindResource("textblock")))
            {
                ContentTemplate = (DataTemplate)FindResource("textblock");
            }
            return(committed);
        }
        private void DoLostFocus()
        {
            KillValidator();

            ValidateExpression(this);

            if (Context != null)
            {   // Unselect if this is the currently selected one.
                ExpressionSelection current = Context.Items.GetValue <ExpressionSelection>();
                if (current != null && current.ModelItem == Expression)
                {
                    ExpressionSelection emptySelection = new ExpressionSelection(null);
                    Context.Items.SetValue(emptySelection);
                }
            }

            // Generate and validate the expression.
            // Get the text from the snapshot and set it to the Text property
            if (ExpressionEditorInstance != null)
            {
                ExpressionEditorInstance.ClearSelection();
            }

            bool committed = false;

            if (!ExplicitCommit)
            {
                //commit change and let the commit change code do the revert
                committed = Commit(false);

                //reset the error icon if we didn't get to set it in the commit
                if (!committed || IsIndependentExpression)
                {
                    EditingState = EditingState.Idle;
                    // Switch the control back to a textbox -
                    // but give it the text from the editor (textbox should be bound to the Text property, so should
                    // automatically be filled with the correct text, from when we set the Text property earlier)
                    if (!ContentTemplate.Equals((DataTemplate)FindResource("textblock")))
                    {
                        ContentTemplate = (DataTemplate)FindResource("textblock");
                    }
                }
            }

            //raise EditorLostLogical focus - in case when some clients need to do explicit commit
            RaiseEvent(new RoutedEventArgs(ExpressionTextBox.EditorLostLogicalFocusEvent, this));
        }
        private void OnEditorUnloaded(object sender, RoutedEventArgs e)
        {
            // Blank the editorSession and the expressionEditor so as not to use up memory
            // Destroy both as you can only ever spawn one editor per session
            if (ExpressionEditorInstance != null)
            {
                //if we are unloaded during editing, this means we got here by someone clicking breadcrumb, we should try to commit
                if (EditingState == EditingState.Editing)
                {
                    Commit(false);
                }
                ExpressionEditorInstance.Close();
            }
            else
            {
                EditingTextBox = null;
            }

            IsEditorLoaded = false;
        }
        private void OnEditorLoaded(object sender, RoutedEventArgs e)
        {
            if (!IsEditorLoaded)
            {
                // If the service exists, create an expression editor and add it to the grid - else switch to the textbox data template
                if (ExpressionEditorService1 != null)
                {
                    Border border = (Border)sender;
                    // Get the references and variables in scope
                    AssemblyContextControlItem assemblies        = (AssemblyContextControlItem)Context.Items.GetValue(typeof(AssemblyContextControlItem));
                    List <ModelItem>           declaredVariables = CSharpExpressionHelper.GetVariablesInScope(OwnerActivity);

                    ImportedNamespaceContextItem importedNamespaces = Context.Items.GetValue <ImportedNamespaceContextItem>();
                    importedNamespaces.EnsureInitialized(Context);
                    //if the expression text is empty and the expression type is set, then we initialize the text to prompt text
                    if (string.Equals(Text, string.Empty, StringComparison.OrdinalIgnoreCase) && ExpressionType != null)
                    {
                        Text = TypeToPromptTextConverter.GetPromptText(ExpressionType);
                    }

                    //this is a hack
                    BlockWidth = Math.Max(ActualWidth - 8, 0);  //8 is the margin
                    if (HasErrors)
                    {
                        BlockWidth = Math.Max(BlockWidth - 16, 0); //give 16 for error icon
                    }
                    try
                    {
                        if (ExpressionType != null)
                        {
                            ExpressionEditorInstance = ExpressionEditorService1.CreateExpressionEditor(assemblies, importedNamespaces, declaredVariables, Text, ExpressionType, new Size(BlockWidth, BlockHeight));
                        }
                        else
                        {
                            ExpressionEditorInstance = ExpressionEditorService1.CreateExpressionEditor(assemblies, importedNamespaces, declaredVariables, Text, new Size(BlockWidth, BlockHeight));
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    if (ExpressionEditorInstance != null)
                    {
                        try
                        {
                            ExpressionEditorInstance.VerticalScrollBarVisibility   = VerticalScrollBarVisibility;
                            ExpressionEditorInstance.HorizontalScrollBarVisibility = HorizontalScrollBarVisibility;

                            ExpressionEditorInstance.AcceptsReturn = AcceptsReturn;
                            ExpressionEditorInstance.AcceptsTab    = AcceptsTab;

                            // Add the expression editor to the text panel, at column 1
                            HostControl = ExpressionEditorInstance.HostControl;

                            // Subscribe to this event to change scrollbar visibility on the fly for auto, and to resize the hostable editor
                            // as necessary
                            ExpressionEditorInstance.LostAggregateFocus += new EventHandler(OnEditorLostAggregateFocus);
                            ExpressionEditorInstance.Closing            += new EventHandler(OnEditorClosing);

                            // Set up Hostable Editor properties
                            ExpressionEditorInstance.MinLines = MinLines;
                            ExpressionEditorInstance.MaxLines = MaxLines;

                            ExpressionEditorInstance.HostControl.Style = (Style)FindResource("editorStyle");

                            border.Child = HostControl;
                            ExpressionEditorInstance.Focus();
                        }
                        catch (KeyNotFoundException ex)
                        {
                            new ApplicationException("Unable to find editor with the following editor name: " + EditorName);
                        }
                    }
                }
                IsEditorLoaded = true;
            }
        }