public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string html = value as string;

            if (string.IsNullOrEmpty(html))
            {
                return(null);
            }

            return(HtmlToXamlConverter.DeserializeFlowDocument(html));
        }
        internal void ShowKnowledge()
        {
            if (ProjectMgr.Instance.App.Knowledge.Content == null)
            {
                return;
            }

#if _SAVE_CONTENT_AS_XAML_
            this.knowledgeDocument.Document = HtmlToXamlConverter.DeserializeFlowDocument(ProjectMgr.Instance.App.Knowledge.Content.Content);
#else
            this.knowledgeDocument.Document = HtmlToXamlConverter.ConvertHtmlToXaml(ProjectMgr.Instance.App.Knowledge.Content.Content);
#endif
        }
Ejemplo n.º 3
0
        internal override System.Windows.Documents.FlowDocument GetKnowledgeDefinition()
        {
            if (this.assessmentApp.Knowledge.Content == null)
            {
                return(new System.Windows.Documents.FlowDocument());
            }

            if (this.assessmentApp.Knowledge.Content.ContentType == Assessment.Data.ContentType.Html)
            {
                return(HtmlToXamlConverter.ConvertHtmlToXaml(this.assessmentApp.Knowledge.Content.Content));
            }
            else if (this.assessmentApp.Knowledge.Content.ContentType == Assessment.Data.ContentType.FlowDocument)
            {
                FlowDocument doc = HtmlToXamlConverter.DeserializeFlowDocument(this.assessmentApp.Knowledge.Content.Content);
                CommonControlCreator.replaceTextBoxWithRichTextBox(doc, this.assessmentApp.Knowledge.Content, null);
                return(doc);
            }

            throw new NotSupportedException("Knowledge must be HTML or FlowDocument format!");
        }
Ejemplo n.º 4
0
        private static void OnBoundDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RichTextBox box = d as RichTextBox;

            if (box == null)
            {
                return;
            }

            RemoveEventHandler(box);

            string newXAML = GetBoundDocument(d);

            if (!string.IsNullOrEmpty(newXAML))
            {
                box.Document = HtmlToXamlConverter.DeserializeFlowDocument(newXAML);
            }

            //box.Document.Blocks.Clear();

            //if (!string.IsNullOrEmpty(newXAML))
            //{
            //    using (MemoryStream xamlMemoryStream = new MemoryStream(Encoding.ASCII.GetBytes(newXAML)))
            //    {
            //        ParserContext parser = new ParserContext();
            //        parser.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            //        parser.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            //        FlowDocument doc = new FlowDocument();
            //        Section section = XamlReader.Load(xamlMemoryStream, parser) as Section;

            //        box.Document.Blocks.Add(section);

            //    }
            //}

            AttachEventHandler(box);
        }
        internal static System.Windows.Documents.Section CreateContentControl(QuestionContent content, string prefix, System.Windows.Documents.Section paragraph, Response response)
        {
            System.Windows.Documents.Section rootSection = paragraph;
            if (rootSection == null)
            {
                rootSection = new System.Windows.Documents.Section();
            }

            if (content.ContentType == ContentType.Html ||
                content.ContentType == ContentType.FlowDocument)
            {
                string doc = string.Empty;
                if (content.ContentType == ContentType.Html)
                {
                    doc = HtmlToXamlConverter.ConvertHtmlToXaml(content.Content, true);
                }
                else
                {
                    doc = content.Content;
                }

                System.Windows.Documents.FlowDocument flowDocument = HtmlToXamlConverter.DeserializeFlowDocument(doc);

                replaceTextBoxWithText(flowDocument, content, null);

                List <Block> tempList = new List <Block>();
                tempList.AddRange(flowDocument.Blocks);
                flowDocument.Blocks.Clear();
                rootSection.Blocks.AddRange(tempList);

                Paragraph firstPara = null;
                if (rootSection.Blocks.FirstBlock is Paragraph)
                {
                    firstPara = rootSection.Blocks.FirstBlock as Paragraph;
                }
                else
                {
                    firstPara = new Paragraph();
                    rootSection.Blocks.InsertBefore(rootSection.Blocks.FirstBlock, firstPara);
                }

                if (firstPara.Inlines.Count > 0)
                {
                    firstPara.Inlines.InsertBefore(firstPara.Inlines.FirstInline, new Run(prefix));
                }
                else
                {
                    firstPara.Inlines.Add(prefix);
                }
            }
            else
            {
                Paragraph para = null;
                if (rootSection.Blocks.LastBlock is Paragraph)
                {
                    para = rootSection.Blocks.LastBlock as Paragraph;
                }
                else
                {
                    para = new Paragraph();
                    rootSection.Blocks.Add(para);
                }

                string questionContent = content.Content;
                int    startIndex      = 0;

                para.Inlines.Add(prefix);

                while (true)
                {
                    startIndex = questionContent.IndexOf("_$", 0);
                    if (startIndex >= 0)
                    {
                        int    endIndex    = questionContent.IndexOf("$_", startIndex);
                        string placeHolder = questionContent.Substring(startIndex, endIndex - startIndex + 2);

                        string text = questionContent.Substring(0, startIndex);

                        if (!string.IsNullOrEmpty(text) &&
                            text[text.Length - 1] == '\n')
                        {
                            para.Inlines.Add(CreateText(text.Remove(text.Length - 1)));
                        }
                        else
                        {
                            para.Inlines.Add(CreateText(text));
                        }

                        QuestionContentPart part = content.GetContentPart(placeHolder);
                        if (response is FIBQuestionResponse)
                        {
                            FIBQuestionResponse fibResponse = response as FIBQuestionResponse;
                            para.Inlines.Add(CreateUIPart(part, fibResponse.GetBlankResponse(part.Id, false)));
                        }
                        else
                        {
                            para.Inlines.Add(CreateUIPart(part, null));
                        }

                        questionContent = questionContent.Remove(0, endIndex + 2);
                        if (string.IsNullOrEmpty(questionContent))
                        {
                            break;
                        }
                    }
                    else
                    {
                        para.Inlines.Add(CreateText(questionContent));
                        break;
                    }
                }
            }

            return(rootSection);
        }
Ejemplo n.º 6
0
        public static Panel CreateContentControl(QuestionContent content, Panel contentPanel, Brush foreground, ContentPartCreated contentPartCreated)
        {
            Panel stackPanel = contentPanel;

            if (stackPanel == null)
            {
                stackPanel = new StackPanel();
                ((StackPanel)stackPanel).Orientation = Orientation.Vertical;
            }

            if (content.ContentType == ContentType.Html ||
                content.ContentType == ContentType.FlowDocument)
            {
                FlowDocumentScrollViewer documentViewer = new FlowDocumentScrollViewer();
                documentViewer.FocusVisualStyle            = null;
                documentViewer.VerticalContentAlignment    = VerticalAlignment.Center;
                documentViewer.VerticalAlignment           = VerticalAlignment.Center;
                documentViewer.HorizontalContentAlignment  = HorizontalAlignment.Left;
                documentViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                documentViewer.Background = Brushes.Transparent;
                documentViewer.FontSize   = 20;

                FlowDocument document = null;
                if (content.ContentType == ContentType.Html)
                {
                    document = HtmlToXamlConverter.ConvertHtmlToXaml(content.Content);
                }
                else
                {
                    document = HtmlToXamlConverter.DeserializeFlowDocument(content.Content);
                }
                replaceTextBoxWithRichTextBox(document, content, contentPartCreated);
                document.FontSize       = 20;
                documentViewer.Document = document;

                stackPanel.Children.Add(documentViewer);
            }
            else
            {
                string questionContent = content.Content;
                if (string.IsNullOrEmpty(questionContent))
                {
                    return(stackPanel);
                }

                int        startIndex = 0;
                StackPanel itemPanel  = new StackPanel();
                itemPanel.Orientation = Orientation.Horizontal;
                stackPanel.Children.Add(itemPanel);
                bool newLine = false;
                while (true)
                {
                    startIndex = questionContent.IndexOf("_$", 0);
                    if (startIndex >= 0)
                    {
                        int    endIndex    = questionContent.IndexOf("$_", startIndex);
                        string placeHolder = questionContent.Substring(startIndex, endIndex - startIndex + 2);

                        string text = questionContent.Substring(0, startIndex);

                        if (!string.IsNullOrEmpty(text))
                        {
                            string[] textParts = text.Split(new char[] { '\n' });
                            if (textParts.Length > 1)
                            {
                                for (int i = 0; i < textParts.Length; i++)
                                {
                                    string temp = textParts[i];
                                    itemPanel.Children.Add(CreateText(temp, foreground));

                                    if (i == textParts.Length - 1)
                                    {
                                        break;
                                    }

                                    StackPanel nextItemPanel = new StackPanel();
                                    nextItemPanel.Orientation = Orientation.Horizontal;
                                    stackPanel.Children.Add(nextItemPanel);
                                    itemPanel = nextItemPanel;
                                }
                            }
                            else
                            {
                                itemPanel.Children.Add(CreateText(text, foreground));
                            }
                        }
                        //else
                        //{
                        //    string[] textParts = text.Split(new char[] { '\n' });
                        //    if (textParts.Length > 1)
                        //    {
                        //        foreach (string temp in textParts)
                        //        {
                        //            itemPanel.Children.Add(CreateText(temp));

                        //            StackPanel nextItemPanel = new StackPanel();
                        //            nextItemPanel.Orientation = Orientation.Horizontal;
                        //            stackPanel.Children.Add(nextItemPanel);
                        //            itemPanel = nextItemPanel;
                        //        }
                        //    }
                        //    else
                        //    {
                        //        itemPanel.Children.Add(CreateText(text));
                        //    }
                        //}

                        if (newLine)
                        {
                            StackPanel nextItemPanel = new StackPanel();
                            nextItemPanel.Orientation = Orientation.Horizontal;
                            stackPanel.Children.Add(nextItemPanel);
                            itemPanel = nextItemPanel;
                        }

                        newLine = false;

                        QuestionContentPart part    = content.GetContentPart(placeHolder);
                        UIElement           element = CommonControlCreator.CreateUIPart(part, foreground);
                        itemPanel.Children.Add(element);
                        if (contentPartCreated != null)
                        {
                            contentPartCreated(element);
                        }

                        questionContent = questionContent.Remove(0, endIndex + 2);
                        if (string.IsNullOrEmpty(questionContent))
                        {
                            break;
                        }
                    }
                    else
                    {
                        string[] textParts = questionContent.Split(new char[] { '\n' });
                        foreach (string text in textParts)
                        {
                            itemPanel.Children.Add(CreateText(text, foreground));

                            StackPanel nextItemPanel = new StackPanel();
                            nextItemPanel.Orientation = Orientation.Horizontal;
                            stackPanel.Children.Add(nextItemPanel);
                            itemPanel = nextItemPanel;
                        }

                        break;
                    }
                }
            }

            return(stackPanel);
        }