Access elements inside of a page

2 posts, 0 answers
  1. Vahid
    Vahid avatar
    4 posts
    Member since:
    Sep 2007

    Posted 10 Dec 2014 Link to this post

    I'm trying to using RadBook control to display a PDF file and also a custom control named DrawingCanvas to draw some shapes over the PDF file loaded.
    I've done this successfully thanks to the vast amount of samples available.
    Here is how I do:

    I load the file as follows:
    private void LoadFile(String filePath)
        documentStream = new FileStream(filePath, FileMode.Open);
        var doc = new PdfFormatProvider(documentStream, FormatProviderSettings.ReadOnDemand).Import();
        book.ItemsSource = doc.Pages;

    And here is the DataTemplate which I use:
    <DataTemplate x:Key="PageTemplate">
                        <ui:FixedDocumentSinglePagePresenter Page="{Binding}" Width="500" Height="500"/>
                        <hosts:DrawingCanvas Width="500" Height="500" Margin="15" Background="#00000000" />
    And everything works great.
    My problem is that, when the page is changed (on PageChanged event), I want to access the DrawingCanvas!
    I tries the 2 approaches mentioned in this post How to access controls in RadBook DataTemplete?, but it doesn't help me.
    When using the loaded event, I have 2 problems:
    1. When paging back, the events (sometimes) does not raise!
    2. It seems the RadBook caches the pages, so while RightPageIndex is 0, I get events of canvases in page 1 and 2.

    And when trying to use the RadBook.ItemContainerGenerator the problem is that myBook.ItemContainerGenerator.ContainerFromIndex(leftPageInd) returns null,
    and I also tried to use the book.Items property, but my Items type are RadFixedPage (because I've set ItemsSource of book to PdfFormatProvider's Pages property) and I don't know how can I use it to get my DrawingCanvas.

    Regards, Vahid 

  2. Martin
    Martin avatar
    1042 posts

    Posted 15 Dec 2014 Link to this post

    Hi Vahid,

    Indeed, the elements in the page templates of RadTileView are loaded only once in the visual tree and therefore their Loaded event is fired only once when their parent page is loaded. However, you should be able to use the second approach from the forum you referenced in your last reply. 

    Basically, you can subscribe for the PageChanged event and inside its handler use the RadTileView.ItemContainerGenerator.ContainerFromIndex() method. 
    private void book_PageChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
        var bookItem = book.ItemContainerGenerator.ContainerFromIndex(book.RightPageIndex) as RadBookItem;
        var canvas = bookItem.ChildrenOfType<DrawingCanvas>().FirstOrDefault();

    For your convenience I attached a simplified project that demonstrates this approach. Can you please give it a try and let me know if I am missing something? 


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. DevCraft R3 2016 release webinar banner
Back to Top