Exemplo n.º 1
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);
        }
        private static void replaceTextBoxWithText(FlowDocument document, QuestionContent content, ContentPartCreated contentPartCreated)
        {
            List <InlineUIContainer> tempList = new List <InlineUIContainer>();

            foreach (var block in document.Blocks)
            {
                if (block is Paragraph)
                {
                    Paragraph para = block as Paragraph;
                    foreach (var inline in para.Inlines)
                    {
                        if (inline is InlineUIContainer)
                        {
                            tempList.Add(inline as InlineUIContainer);
                        }
                    }
                }
            }

            foreach (var inlineUIContainer in tempList)
            {
                if (inlineUIContainer.Child is Image) // replace image url with local url.
                {
                    Image image = inlineUIContainer.Child as Image;

                    Image newImage = new Image();

                    Uri         newUri = new Uri(CommonControlCreator.getImageFile(image.Tag as string, content));
                    BitmapImage newBi  = new BitmapImage(newUri);
                    newImage.Source         = newBi;
                    newImage.Width          = (1.0 == newBi.Width ? 50 : newBi.Width);
                    newImage.Height         = (1.0 == newBi.Height ? 50 : newBi.Height);
                    newImage.Tag            = image.Tag;
                    inlineUIContainer.Child = newImage;
                }
                else if (inlineUIContainer.Child is TextBox) // replace textbox with richtextbox
                {
                    TextBox textBox = inlineUIContainer.Child as TextBox;

                    TextBlock richTextBox = new TextBlock();
                    richTextBox.Text        = "(                )";
                    richTextBox.Tag         = textBox.Tag;
                    inlineUIContainer.Child = richTextBox;
                    if (contentPartCreated != null)
                    {
                        contentPartCreated(richTextBox);
                    }
                }
            }
        }
Exemplo n.º 3
0
        internal static Panel CreateContentControl(QuestionContent content, Panel contentPanel, ContentPartCreated contentPartCreated)
        {
            Panel stackPanel = contentPanel;

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

            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));

                                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));
                        }
                    }
                    //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);
                    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));

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

                    break;
                }
            }

            return(stackPanel);
        }