[英]Make a Part of Text Bold inside TextBlock
I know that we can use <Run>
in XAML to achieve what I am asking : 我知道我们可以在XAML中使用<Run>
来实现我的要求:
<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: 假设我的数据库中有以下Text:
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
. 我希望text between <b> and </b> to be bold
的text between <b> and </b> to be bold
。 How can I achieve this? 我怎样才能做到这一点?
If you want to display HTML, use a Webbrowser control. 如果要显示HTML,请使用Web浏览器控件。
<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
}
It looks like you want to replace your custom formatting with <Bold>
- see TextBlock for more info. 看起来您想要用<Bold>
替换自定义格式 - 有关详细信息,请参阅TextBlock 。 Sample from the article: 文章样本:
<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 TextBlock
expects. 一种方法是重新格式化字符串以匹配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),您甚至可以使用基本的String.Replace
:text = text.Replace(“”,“”)`。
If you have you own custom formatting (like *boldtext*
) you'll need to invent custom parser for that. 如果您拥有自己的自定义格式(如*boldtext*
),则需要为此创建自定义解析器。
You can subscribe to TargetUpdated
event: 您可以订阅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
: 和TextBlock
XAML:
<TextBlock x:Name="textBlock" Text="{Binding NotifyOnTargetUpdated=True}"></TextBlock>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.