public static void UpdateWith(this TextBlock textBlock, string htmlInput, IParamParser paramParser) { var tree = new HtmlTagTree(paramParser, _builtinTags); tree.BuildFrom(htmlInput); // update textbox with inline elements according to syntax tree items var context = new InlineCreationContext(); foreach (var tag in tree.GetTags()) { switch (_builtinTags[tag.ID].Flags) { case HTMLFlag.TextFormat: context.UpdateStyle(tag); break; case HTMLFlag.Element: var inline = tag.CreateInline(textBlock, context); textBlock.Inlines.Add(inline); break; } } }
private Inline CreateInlineInternal(TextBlock textBlock, InlineCreationContext currentStateType) { switch (Name) { case "br": return(new LineBreak()); case "binding": case "text": Inline result; if (Name == "binding") { result = new Bold(new Run("{Binding}")); if (Contains("path") && (textBlock.DataContext != null)) { var dataContext = textBlock.DataContext; var propertyName = _variables["path"]; var property = dataContext.GetType().GetProperty(propertyName); if (property != null && property.CanRead == true) { var value = property.GetValue(dataContext, null); result = new Run(value?.ToString() ?? ""); } } } else { result = new Run(_variables["value"]); } if (currentStateType.SubScript) { result.SetCurrentValue(Typography.VariantsProperty, FontVariants.Subscript); } if (currentStateType.SuperScript) { result.SetCurrentValue(Typography.VariantsProperty, FontVariants.Superscript); } if (currentStateType.Bold) { result = new Bold(result); } if (currentStateType.Italic) { result = new Italic(result); } if (currentStateType.Underline) { result = new Underline(result); } if (currentStateType.Foreground.HasValue) { result.Foreground = new SolidColorBrush(currentStateType.Foreground.Value); } return(result); case "img": if (!Contains("source")) { return(new Run()); } var width = double.NaN; if (Contains("width") && double.TryParse(_variables["width"], out var internal_width)) { width = internal_width; } var height = double.NaN; if (Contains("height") && double.TryParse(_variables["height"], out var internal_height)) { height = internal_height; } if (!Uri.TryCreate(_variables["source"], UriKind.RelativeOrAbsolute, out var uri)) { return(new Run()); } var bitmap = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad }; bitmap.BeginInit(); bitmap.UriSource = uri; bitmap.EndInit(); // TODO fetch and apply style var image = new Image { Source = bitmap, Width = width, Height = height }; if (Contains("href")) { return(TryWrapInHyperLink(new InlineUIContainer(image), _variables["href"])); } return(new InlineUIContainer(image)); default: return(new Run()); } }
public Inline CreateInline(TextBlock textBlock, InlineCreationContext currentStateType) { var result = CreateInlineInternal(textBlock, currentStateType); return(TryWrapInHyperLink(result, currentStateType.HyperLink?.Trim('\"'))); }