我想要包含Inline标记的字符串,例如
var str = "foo bar <Bold>dong</Bold>"
Run Code Online (Sandbox Code Playgroud)
并向 TextBlock 提供它,以便文本的格式就像添加到 Inlines 集合中一样。我怎样才能做到这一点?
您可以使用标签包装文本<TextBlock>并将整个内容解析为 XAML:
public TextBlock CreateTextBlock(string inlines)
{
var xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ inlines + "</TextBlock>";
return XamlReader.Parse(xaml) as TextBlock;
}
Run Code Online (Sandbox Code Playgroud)
然后根据需要使用新创建的 TextBlock。把它放在某个面板中
var str = "foo bar <Bold>dong</Bold>";
grid.Children.Add(CreateTextBlock(str));
Run Code Online (Sandbox Code Playgroud)
或者将其复制Inlines到另一个 TextBlock。