How to access controls in RadBook DataTemplete?

2 posts, 0 answers
  1. Eric
    Eric avatar
    2 posts
    Member since:
    Nov 2008

    Posted 29 Feb 2012 Link to this post


    I using a RadBook controls with some custom datatemplates that contain a few controls.

    My question is: How do I access my "PhotoImage" control inside my define DataTempletes using code-behind (C#)?
         
       <!--  Declare the template used for the left pages  -->
            <DataTemplate x:Name="LeftPageTemplate">
                <StackPanel Margin="10" Background="LightGray">

                    <TextBlock FontSize="24"
                               FontWeight="Bold"
                               Text="{Binding Title}" />
                    <Image Width="240"
                           Height="320"
                           Source="{Binding Image}" />
                    <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">

                        <Image  x:Name="PhotoImage" Source="{Binding Photo}" />

                        <TextBlock FontWeight="Bold" Text="Date Taken:" />
                        <TextBlock Text="{Binding DateTaken}" />
                    </StackPanel>
                    <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
                        <TextBlock FontWeight="Bold" Text="Size:" />
                        <TextBlock Text="{Binding Size}" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
      

  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 05 Mar 2012 Link to this post

    Hello Eric,

    You can either define a Loaded event handler for the Image control in your DataTemplate like this:
    <DataTemplate x:Name="LeftPageTemplate">
        <StackPanel Margin="10" Background="LightGray">
     
            <TextBlock FontSize="24" FontWeight="Bold" Text="Title" />
            <Image Width="240" Height="320" Source="{Binding PhotoSource}"  />
            <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
     
                <Image x:Name="PhotoImage" Source="{Binding PhotoSource}" Width="20" Height="20" Loaded="Image_Loaded"/>
     
                <TextBlock FontWeight="Bold" Text="Date Taken:" />
                <TextBlock Text="12.12.12" />
            </StackPanel>
            <StackPanel HorizontalAlignment="Stretch" Orientation="Horizontal">
                <TextBlock FontWeight="Bold" Text="Size:" />
                <TextBlock Text="150" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    and implement your custom logic when the image as soon as the image is loaded.

    Or you can access the Image through the current left page. You can find the index of the currently displayed left page through the RadBook.RightPageIndex. The left page index would be:
    int leftPageInd = myBook.RightPageIndex-1;
    Then you need to get the RadBookItem container and you can use the RadBook.ItemContainerGenerator to get a RadBookItem container from an index:
    RadBookItem item = myBook.ItemContainerGenerator.ContainerFromIndex(leftPageInd) as RadBookItem;
    You can traverse the visual children of that item to find the Image:
    Image img = item.ChildrenOfType<Image>().Where(i => i.Name == "PhotoImage").FirstOrDefault();
    if (img != null)
    {
        //implement custom logic
    }

    Give this a try and let us know if it helps.

    All the best,
    Tina Stancheva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
Back to Top