我正在尝试将Text设置为通用应用中的RichTextBlock,这是我在xaml中的代码:
<RichTextBlock x:Name="descr">
<Paragraph>
<Paragraph.Inlines>
<Run Text="{Binding Path=desc}"/>
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock>
但我不知道如何在代码后面的RichTextBlock中设置Text,这是我的尝试:
Paragraph p = new Paragraph();
p.Inlines.Add("test");//error here cannot convert from 'string' to 'Windows.UI.Xaml.Documents.Inline'
descr.Blocks.Add(p);
那么如何在C#后面的代码中将Text设置为RichTextBlock 谢谢你的帮助
答案 0 :(得分:5)
Inlines属性是InlineCollection,它是一个集合 当您尝试向此集合添加字符串时,Inline个对象。
内联的MSDN
为内联文本元素提供基类,例如Span和Run。
因此,您需要添加Run或Span对象。
// Create run and set text
Run run = new Run();
run.Text = "test";
// Create paragraph
Paragraph paragraph = new Paragraph();
// Add run to the paragraph
paragraph.Inlines.Add(run);
// Add paragraph to the rich text block
richTextBlock.Blocks.Add(paragraph);
修改强>
好像你不能直接从后面的代码绑定Run或Span对象的Text属性。