我看过其他一些关于此的帖子,但没有看到任何可以解决我的问题的帖子。基本上,我需要将一些文本(粗体和非粗体)很好地包裹在 Stackpanel
或 Wrappanel
中。这是一个视觉效果:
我需要您看到的两个 TextBlocks
的组合。我有第一个粗体 TextBlock
,其中包含“Title:”,然后在同一行上我需要下一个 TextBlock
,它可能会换行,也可能不会换行。如果确实换行,我需要顶行保持在顶行然后换行,就好像它们都是一个 TextBlock
一样。这是我需要的自制油漆视觉效果:
这是我到目前为止的代码:
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here " />
</toolkit:WrapPanel>
<toolkit:WrapPanel Orientation="Horizontal">
<TextBlock Text="Title: " FontWeight="Bold"/>
<TextBlock TextWrapping="Wrap" Text="More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :( " />
</toolkit:WrapPanel>
现在,我不反对在one TextBlock
中完成此操作(如果可能)。我知道使用 TextBlock.Inlines
我可以添加一个 Run
粗体文本,然后添加另一个常规文本。问题是 Inlines
cannot be bound使用 MVVM(我没有在本次演示中使用,但将在最终代码中使用)。
因此无论解决方案是什么,我都需要能够从代码隐藏中设置 TextBlock
的值,以便我知道可以使用 MVVM 完成。
有谁知道实现这个的方法吗?
最佳答案
使用 RichTextBox
控件对每种字体样式进行不同的运行。
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold">Title:</Run>
<Run>More text goes here and I want it to wrap lines and go underneath the title but I can't get it to to do that. :(</Run>
</Paragraph>
</RichTextBox>
您可以将 Run
元素的 Text 属性绑定(bind)到您的数据上下文,例如:
<RichTextBox>
<Paragraph>
<Run FontWeight="Bold"
Text="{Binding Header}"></Run>
<Run Text="{Binding Text}"></Run>
</Paragraph>
</RichTextBox>
关于xaml - 在水平堆栈面板或包装面板中很好地包装文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21567153/