Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RichTextBlock in FlipView in Metro Style App

I am using standard Visual Studio templates and I have a ItemsDetailPage that contains a FlipView with a RichTextBlock in its DataTemplate.

I want to set the RichTextBlock block to my custom Paragraphs generated in text. I think there is no way to bind RichTextBlocks Block in XAML so I am using code behind. In the Loaded event of RichTextBlock I set its Block, that works ok. But the problem is, the Loaded event gets called only once when the page is displayed. When I "flip" to another item, the selected item of the FlipView changes but the Loaded event does not get called again (I think this is ok).

I tried setting the RichTextBlock in the FlipViews SelectionChanged item but that does not work.

var ind = this.flipView.SelectedIndex;

        var flipViewItem = this.flipView.ItemContainerGenerator.ContainerFromIndex(flipView.SelectedIndex);

        if (flipViewItem != null)
        {               
            var scroller = FindFirstElementInVisualTree<ScrollViewer>(flipViewItem);
            var tb = scroller.FindDescendantByName("richTextColumns").FindDescendantByName("richTextBlock") as RichTextBlock;
            SetRichContent(tb, (flipView.SelectedItem as ArticleViewModel).HtmlContent);               
        }

The SetRichContent gets called, sets the RichTextBlocks Blocks but visually they do not change and after a few flips, the whole app crashes without any additional information.

So my question is, how do I get my own code called on the RichTextBlock with each flip (seleced item change)?

like image 496
Igor Kulman Avatar asked Nov 12 '22 21:11

Igor Kulman


1 Answers

You can bind rich text boxes. Make sure your data context is set properly. We need to see more code to make an appropriate answer.

<RichTextColumns>
     <RichTextColumns.ColumnTemplate>
         <DataTemplate>
             <RichTextBlockOverflow Width="400" Margin="50,0,0,0"/>
         </DataTemplate>
    </RichTextColumns.ColumnTemplate>

     <RichTextBlock Width="400">
         <Paragraph>
             <Run Text="{Binding Content}"/>
         </Paragraph>
     </RichTextBlock>
</RichTextColumns>
like image 193
Dreambeats Avatar answered Nov 15 '22 13:11

Dreambeats