public void GenerateVisualTree () { if (!HasChildren) return; var children = new List<Element>(); InlineContainer inlines = null; foreach (Element el in _children.SelectMany(c => c.GenerateVisualElements())) { var inlineEl = el as InlineElement; // Add inline child. if (inlineEl != null) { // Add inline child to inline element. if (this is InlineElement) { VisualChildren.Add(inlineEl); } // Add inline child to block element. else { // Group inline children into single inline container. if (inlines == null) { inlines = new InlineContainer((BlockElement)this); children.Add(inlines); } inlines.Children.Add(inlineEl); } } // Add block or generator child. else { if (inlines != null) { // Close inline container. inlines.VisualChildren = inlines.Children.ToList(); inlines = null; } children.Add(el); } // Recurse. el.GenerateVisualTree(); } // Close inline container (if not closed within foreach). if (inlines != null) { inlines.VisualChildren = inlines.Children.ToList(); } // Let element decide how to add children: directly or grouped into stack. SetVisualChildren(children); }
protected virtual void SetVisualChildren ([NotNull] IList<Element> visualChildren) { if (visualChildren == null) throw new ArgumentNullException(nameof(visualChildren)); if (visualChildren.Count == 1) { VisualChildren = visualChildren; } else if (visualChildren.Count > 1) { VisualChildren = new List<Element>(1) { new Stack { DataContext = DataContext, VisualChildren = visualChildren, } }; } }