This is a migrated thread and some comments may be shown as answers.

Access elements inside of a page

1 Answer 54 Views
Book
This is a migrated thread and some comments may be shown as answers.
Vahid
Top achievements
Rank 1
Vahid asked on 10 Dec 2014, 02:50 PM
Hi
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">
            <Viewbox>
                     <Grid>
                    <ui:FixedDocumentSinglePagePresenter Page="{Binding}" Width="500" Height="500"/>
                    <hosts:DrawingCanvas Width="500" Height="500" Margin="15" Background="#00000000" />
                </Grid>
            </Viewbox>
        </DataTemplate>
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 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 15 Dec 2014, 09:18 AM
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? 

Regards,
Martin
Telerik
 

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.

 
Tags
Book
Asked by
Vahid
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or