use*_*206 1 c# win-universal-app uwp windows-10-universal
我试图在通用应用程序中将Text设置为RichTextBlock,这是我在xaml中的代码:
<RichTextBlock x:Name="descr">
<Paragraph>
<Paragraph.Inlines>
<Run Text="{Binding Path=desc}"/>
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock>
Run Code Online (Sandbox Code Playgroud)
但我不知道如何在代码背后的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);
Run Code Online (Sandbox Code Playgroud)
那么如何在C#后面的代码中将Text设置为RichTextBlock,感谢您的帮助
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 Code Online (Sandbox Code Playgroud)
编辑
好像你不能直接从后面的代码绑定Run或Span对象的Text属性.