wpf 在 TextBlock 中加粗文本的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20819750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a Part of Text Bold inside TextBlock
提问by Khushi
I know that we can use <Run>in XAML to achieve what I am asking :
我知道我们可以<Run>在 XAML 中使用来实现我的要求:
<TextBlock.Inlines>
<Run Text="This is" />
<Run FontWeight="Bold" Text="Bold Text." />
</TextBlock.Inlines>
Also I can do it in code behind as follows:
我也可以在后面的代码中做到这一点,如下所示:
TextBlock.Inlines.Add(new Run("This is"));
TextBlock.Inlines.Add(new Bold(new Run("Bold Text.")));
But my problem is something different:
但我的问题有所不同:
Suppose I have following Text in my database:
假设我的数据库中有以下文本:
This is <b>Bold Text</b>.
Now, my Textblock is bound to a field that contains the above text in database.
现在,我的 Textblock 绑定到包含数据库中上述文本的字段。
I want the text between <b> and </b> to be bold. How can I achieve this?
我想要text between <b> and </b> to be bold. 我怎样才能做到这一点?
回答by Thomas Weller
If you want to display HTML, use a Webbrowser control.
如果要显示 HTML,请使用 Webbrowser 控件。
<WebBrowser Name="myWebBrowser"/>
And in your code, pass your text like this:
在你的代码中,像这样传递你的文本:
myWebBrowser.NavigateToString(myHTMLString);
If not, and bold is the only thing to be done and cannot be nested, you can do it like this:
如果没有,并且大胆是唯一要做的事情并且不能嵌套,你可以这样做:
string s = "<b>This</b> is <b>bold</b> text <b>bold</b> again."; // Sample text
var parts = s.Split(new []{"<b>", "</b>"}, StringSplitOptions.None);
bool isbold = false; // Start in normal mode
foreach (var part in parts)
{
if (isbold)
myTextBlock.Inlines.Add(new Bold(new Run(part)));
else
myTextBlock.Inlines.Add(new Run(part));
isbold = !isbold; // toggle between bold and not bold
}
回答by Alexei Levenkov
It looks like you want to replace your custom formatting with <Bold>- see TextBlockfor more info. Sample from the article:
看起来您想将自定义格式替换为<Bold>-有关详细信息,请参阅TextBlock。文章示例:
<TextBlock Name="textBlock1" TextWrapping="Wrap">
<Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>,
and is geared specifically at integrating <Italic>small</Italic> portions
of flow content into a UI.
</TextBlock>
One approach is to re-format string to match what TextBlockexpects.
一种方法是重新格式化字符串以匹配TextBlock预期的内容。
If you have HTML input - parse the text with HtmlAgilityPack first and than walk though resulting elements and construct string with b-elements replaced with text wrapped <Bold>and similar to other formatting.
如果您有 HTML 输入 - 首先使用 HtmlAgilityPack 解析文本,然后遍历结果元素并构造字符串,其中b-elements 替换为文本包装<Bold>并类似于其他格式。
If database content is known to have only valid begin/end pairs (not random HTML) you may even get away with basic String.Replace: text = text.Replace( "", "")`.
如果已知数据库内容只有有效的开始/结束对(不是随机 HTML),您甚至可以使用 basic String.Replace: text = text.Replace( "", "")`。
If you have you own custom formatting (like *boldtext*) you'll need to invent custom parser for that.
如果您有自己的自定义格式(如*boldtext*),则需要为此发明自定义解析器。
回答by w.b
You can subscribe to TargetUpdatedevent:
您可以订阅TargetUpdated事件:
void textBlock_TargetUpdated(object sender, DataTransferEventArgs e)
{
string text = textBlock.Text;
if (text.Contains("<b>"))
{
textBlock.Text = "";
int startIndex = text.IndexOf("<b>");
int endIndex = text.IndexOf("</b>");
textBlock.Inlines.Add(new Run(text.Substring(0, startIndex)));
textBlock.Inlines.Add(new Bold(new Run(text.Substring(startIndex + 3, endIndex - (startIndex + 3)))));
textBlock.Inlines.Add(new Run(text.Substring(endIndex + 4)));
}
}
and XAML for the TextBlock:
和 XAML 用于TextBlock:
<TextBlock x:Name="textBlock" Text="{Binding NotifyOnTargetUpdated=True}"></TextBlock>

