locked
Set content of RichTextBlock Dynamically by Binding or simply by code

    Question

  • I am trying to set the content of RichTextBlock dynamically. I have tried Data Binding and also by adding Paragraph element as child but found no solution. Can somebody provide some solution or snippet for it?
    Thursday, January 08, 2015 11:56 AM

Answers

  • Hi Rohitrkkumar,

    It seems that RichTextBlock doesn’t provide binding functionality. You can try the following approach to work around.

    Step 1, build a ContnetControl to instead of RichTextBlock like the following code snippet.

    <ContentControl Content="{Binding Path=Snippet, Converter={StaticResource RtFToBlocks}}">
    
                </ContentControl>
    

    Step 2, prepare RTF string like

    <RichTextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Bold>Fearful</Bold> of all things, Fred...</Paragraph></RichTextBlock>

    Step 3, use value converter to bind to ContentControl.

    public object Convert(object value, Type targetType, object parameter, string language)
    
            {
    
                string rtfText = (string)value;
    
                object blocksObj = XamlReader.Load(rtfText);
    
                return (RichTextBlock)blocksObj;
    
            }
    

    Please let me know if you have any concerns.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, January 09, 2015 2:14 AM
    Moderator