Pass the Page.Datacontext to a UserControl

3 posts, 0 answers
  1. Mike
    Mike avatar
    26 posts
    Member since:
    Dec 2006

    Posted 29 Sep 2010 Link to this post

    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">
    I have a Page Selector that loads the appropriate template:
    <local:PageTemplateSelector x:Key="PageSelector"
        DocTemplate="{StaticResource DocTemplate}"

    In my Code behind in sub new I have:
    For x = 1 To 500
        _Pages.Add(New PageClass(PageClass.PageClassType.Doc, x))
    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?

  2. Mike
    Mike avatar
    26 posts
    Member since:
    Dec 2006

    Posted 30 Sep 2010 Link to this post

    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.

  3. Kiril Stanoev
    Kiril Stanoev avatar
    1512 posts

    Posted 05 Oct 2010 Link to this post

    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
Back to Top