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

Pass the Page.Datacontext to a UserControl

2 Answers 117 Views
Book
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 30 Sep 2010, 12:49 AM
I have a book that loads 1 of 5 templates dynamically based on the PageItem, no problem there.
Each Template hosts a user control. I need to pass the page.datacontext to the usercontrol. I have a public property on the user control to accept the data but don't know how to get it there.
So one of my templates is:
<DataTemplate x:Key="DocTemplate">
    <Grid>
        <pmbUC:ctrlDoc></pmbUC:ctrlDoc>
    </Grid>
</DataTemplate>
I have a Page Selector that loads the appropriate template:
<local:PageTemplateSelector x:Key="PageSelector"
    DocTemplate="{StaticResource DocTemplate}"
    .
    .
    .
    .
>
</local:PageTemplateSelector>

In my Code behind in sub new I have:
For x = 1 To 500
    _Pages.Add(New PageClass(PageClass.PageClassType.Doc, x))
Next
RadBook1.ItemsSource = _Pages
(I also load a bunch of other types, but you get the point)

So far so good, the book loads and the correct template is displayed on the page
So now how do I get the Page.DataContext to the user control. I was looking at PreviewPageFlipStarted since I have to do some validation there anyway. Is that where I would do this or is there a simpler way?
Am I going down the wrong path with Page.DataContext? Can I just some how pass the current Item from the ItemSource to the control?

Thanks
 Mike

2 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 30 Sep 2010, 04:42 PM
I figured out an approach that works, but not sure if it is the best way.
Instead of binding to a collection onf objects and then trying to get the object to the user control on flip, I just created new BookItems with the content set to the usercontrol and pass the object to the usercontrol in the contructor and added the BookItems to the Items collection on initialize of the xaml page.
It seems to work just fine.

 Mike
0
Kiril Stanoev
Telerik team
answered on 05 Oct 2010, 08:07 AM
Hello Mike,

What you are describing is one possible way to accomplish this scenario. I'm attaching my way of solving this case. Have a look at it and let me know if you have further questions or comments.

Sincerely yours,
Kiril Stanoev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Book
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Kiril Stanoev
Telerik team
Share this question
or