I know that we can use <Run>
in XAML to achieve what I am asking :
<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.
I want the text between <b> and </b> to be bold
. How can I achieve this?
If you want to display HTML, use a Webbrowser control.
<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
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments