private void simpleButtonOk_Click(object sender, EventArgs e)
        {
            string fieldCode = InsertFieldRichEditHelper.CurrentVariableType;
            IFieldParametersDialog parametersDialog = InsertFieldRichEditHelper.VariableTypes[InsertFieldRichEditHelper.CurrentVariableType] as IFieldParametersDialog;

            if (parametersDialog != null)
            {
                string      parametersString = parametersDialog.GenerateFieldParametersString();
                SubDocument currentDocument  = richEditControl.Document.CaretPosition.BeginUpdateDocument();

                if (parametersDialog is ucIFFIELD)
                {
                    ucIFFIELD IFFieldInsertDialog = parametersDialog as ucIFFIELD;
                    if (IFFieldInsertDialog.IsFieldParametersValid())
                    {
                        Field newField = currentDocument.Fields.Create(richEditControl.Document.CaretPosition, fieldCode + " " + parametersString);

                        List <string> nestedFiueldsList = IFFieldInsertDialog.GetNestedFieldsList();

                        foreach (string nestedFieldCode in nestedFiueldsList)
                        {
                            DocumentRange[] ranges = currentDocument.FindAll(nestedFieldCode, SearchOptions.WholeWord);
                            foreach (DocumentRange range in ranges)
                            {
                                currentDocument.Fields.Create(range);
                            }
                        }
                    }
                }
                else
                {
                    if (parametersString != InsertFieldRichEditHelper.EmptyFieldCode)
                    {
                        currentDocument.Fields.Create(richEditControl.Document.CaretPosition, fieldCode + " " + parametersString);
                    }
                    else
                    {
                        currentDocument.Fields.Create(richEditControl.Document.CaretPosition, fieldCode);
                    }
                }

                currentDocument.Fields.Update();
                richEditControl.Document.CaretPosition.EndUpdateDocument(currentDocument);
            }
            Close();
        }
        static void buttonInsertFieldDialog_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (FieldDialog == null)
            {
                FieldDialog = new InsertFieldDialog();
                Application curApp     = Application.Current;
                Window      mainWindow = curApp.MainWindow;
                FieldDialog.Owner = mainWindow;
                FieldDialog.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            }
            MessageBoxResult result = FieldDialog.ShowDialog(MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                IFieldParametersDialog parametersDialog = CurrentParametersDialog;
                if (parametersDialog != null)
                {
                    string fieldCode = InsertFieldRichEditHelper.CurrentVariableType;

                    string parametersString = parametersDialog.GenerateFieldParametersString();
                    if (parametersString == InsertFieldRichEditHelper.EmptyFieldCode)
                    {
                        return;
                    }

                    SubDocument currentDocument = CurrentRichEditControl.Document.CaretPosition.BeginUpdateDocument();

                    if (parametersDialog is ucIFFIELD)
                    {
                        ucIFFIELD IFFieldInsertDialog = parametersDialog as ucIFFIELD;
                        if (IFFieldInsertDialog.IsFieldParametersValid())
                        {
                            Field newField = currentDocument.Fields.Create(CurrentRichEditControl.Document.CaretPosition, fieldCode + " " + parametersString);

                            List <string> nestedFiueldsList = IFFieldInsertDialog.GetNestedFieldsList();

                            foreach (string nestedFieldCode in nestedFiueldsList)
                            {
                                DocumentRange[] ranges = currentDocument.FindAll(nestedFieldCode, SearchOptions.WholeWord);
                                foreach (DocumentRange range in ranges)
                                {
                                    currentDocument.Fields.Create(range);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (parametersString != "")
                        {
                            currentDocument.Fields.Create(CurrentRichEditControl.Document.CaretPosition, fieldCode + " " + parametersString);
                        }
                        else
                        {
                            currentDocument.Fields.Create(CurrentRichEditControl.Document.CaretPosition, fieldCode);
                        }
                    }

                    currentDocument.Fields.Update();
                    CurrentRichEditControl.Document.CaretPosition.EndUpdateDocument(currentDocument);
                }
            }
        }