I have a TextBlock bound to a property. However, I’d like to bold certain words within the text. What is the easiest way to do this? FYI, I’m new to WPF.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Bolding individual words involves actually creating more inline elements, so you can’t just bind a string to the TextBlock’s Text and do this.
What I’ve done for this in the past is created a subclass of TextBlock which has a custom property that I bind to. When this property is bound I clear the Inlines of the base TextBlock and then use an algorithm that parses the new string value creating either plain Runs, Bolds, Hyperlinks etc.
Here’s some sample code which I wrote for my experimental Twitter client which detects URLs, emails and @ pattern and creates hyperlinks for them. Regular text is inlined as normal runs: