WPF TextBlock dynamic bold and italic parts

    General discussion

  • Hello,

    I'm using MVVM pattern and I have string type property in my ModelView.

    The string may contain following HTML tags:

    <b>, </b>, <i>, </i>

    I need to make certain parts of text in TextBlock to be in normal, bold or italic.

    At moment I have created workaround, a helper method that works like this:

    1. Breaks HTML string into parts

    2. Creates instance of Run class

    3. Depending on tag, sets FontWeight or FontStyle properties

    4. Adds instance of Run class to TextBlock's Inlines collection

    This solution works, but it is not compatible with MVVM pattern.

    I was thinking of using Convertors, but I'm not sure what property of TextBlock should I do binding to.

    What do You think, how can this problem be solved?

    • Changed type Jim Zhou - MSFT Monday, April 06, 2009 8:34 AM custmer does not provide feedback
    Tuesday, March 31, 2009 5:08 PM

All replies

  • Well I have to give the credit to DrWPF since I got this originally from him, but this how I do the different FontWeight's in a TextBlock:

    <TextBlock >
                                This is the normal part and 
                                <Span FontWeight="Bold">this is the bold part</Span>
                                inside the normal font sent.</TextBlock>

    Hope this helps you to what you want. It will at least give you some ideas.

    Tuesday, March 31, 2009 6:06 PM
  • Hi,

    Try to use RelativeSource.Self syntax to bind the TextBl
    k itself.


    Jim Zhou -MSFT
    Thursday, April 02, 2009 6:33 AM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to "Question” by clicking the "Options" link at the top of your post, and selecting "Change Type" menu item from the pop menu. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.


    Thank you!

    Jim Zhou -MSFT
    Monday, April 06, 2009 8:33 AM