构建具有粗体和斜体标记文本的 FrameworkElement 最简单的方法是什么?
有没有更优雅的方法来执行以下操作?
基本上,我需要一种简单的方法来以编程方式构建一个WrapPanel
(或其他FrameworkElement):
- 正确换行
- 允许某些单词具有粗体文本
- 允许某些单词具有斜体文本
- 允许其他格式,例如颜色,理想的背景
- 是将“
This is bold and this is italic text.
”转换为适当的 FrameworkElement 的方法,因此我例如,可以将其添加到 StackPanel 并显示它。
代码:
using System.Windows;
using System.Windows.Controls;
namespace TestAddTextBlock2343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
WrapPanel wp = new WrapPanel();
wp.AddTextBlock("This is a sentence with ");
{
TextBlock tb = wp.AddTextBlockAndReturn("bold text");
tb.FontWeight = FontWeights.Bold;
}
wp.AddTextBlock(" and ");
{
TextBlock tb = wp.AddTextBlockAndReturn("italic text");
tb.FontStyle = FontStyles.Italic;
}
wp.AddTextBlock(" in it.");
}
}
public static class XamlHelpers
{
public static TextBlock AddTextBlockAndReturn(this WrapPanel wp, string text)
{
TextBlock tb = new TextBlock();
tb.Text = text;
wp.Children.Add(tb);
return tb;
}
public static void AddTextBlock(this WrapPanel wp, string text)
{
TextBlock tb = wp.AddTextBlockAndReturn(text);
}
}
}
Is there any more elegant way to do the following?
Basically I need an easy way to programatically build a WrapPanel
(or other FrameworkElement) that:
- wraps correctly
- allows some words to have bold text
- allows some words to have italic text
- allows other formatting, e.g. color, background
- ideal would be some method that converts e.g. "
This is <b>bold</b> and this is <i>italic</i> text.
" into an appropriate FrameworkElement so I can e.g. add it to a StackPanel and display it.
Code:
using System.Windows;
using System.Windows.Controls;
namespace TestAddTextBlock2343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
WrapPanel wp = new WrapPanel();
wp.AddTextBlock("This is a sentence with ");
{
TextBlock tb = wp.AddTextBlockAndReturn("bold text");
tb.FontWeight = FontWeights.Bold;
}
wp.AddTextBlock(" and ");
{
TextBlock tb = wp.AddTextBlockAndReturn("italic text");
tb.FontStyle = FontStyles.Italic;
}
wp.AddTextBlock(" in it.");
}
}
public static class XamlHelpers
{
public static TextBlock AddTextBlockAndReturn(this WrapPanel wp, string text)
{
TextBlock tb = new TextBlock();
tb.Text = text;
wp.Children.Add(tb);
return tb;
}
public static void AddTextBlock(this WrapPanel wp, string text)
{
TextBlock tb = wp.AddTextBlockAndReturn(text);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我在另一个答案中发现
TextBlock
也有一个Inlines
集合,可以向其中添加Run
。 Anvaka的答案巧妙地使用附加属性作为一种转换器。我认为适合您情况的是
FlowDocumentScrollViewer
和FlowDocument
。我描述了通过IValueConverter
手动创建一个 此处。您可能会使用与示例中所示类似的辅助函数,但
FlowDocument
已经非常类似于 HTML,并且可以轻松处理换行。您将
Paragraph
添加到FlowDocument
,将Run
添加到Paragraph
,并且每个Run
派生自TextElement
,因此它具有许多与TextBlock
相同的属性。此外,如果格式化子字符串仍仅限于粗体/斜体标记或其他一些极其简单的标记,则使用 Regex.Split() 可能是确定单独的 Run 的最简单方法 来自单个字符串。它允许您将一个字符串拆分为多个字符串,但保留“分隔符”。
Edit: I discovered in a different answer that the
TextBlock
also has anInlines
collection to which one can addRun
s. Anvaka's answer ingeniously uses an attached property as a sort of converter.What I think will suit your situation is a
FlowDocumentScrollViewer
and aFlowDocument
. I describe manual creation of one through anIValueConverter
a little bit here.You'll likely use similar helper functions as you've shown in your example, but the
FlowDocument
is already much like HTML and will handle wrapping effortlessly.You add
Paragraph
s to theFlowDocument
, you addRun
s to theParagraph
, and eachRun
derives fromTextElement
so it has a lot of the same properties thatTextBlock
s do.Also, if the formatting substrings are going to remain restricted to bold/italic tags or some other extremely simple markup, use of
Regex.Split()
might be the easiest way to determine the separateRun
s from a single string. It allows you to split a string into multiple strings but keep your "delimiters".