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

How to access controls in RadBook DataTemplete?

1 Answer 76 Views
Book
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 29 Feb 2012, 10:02 PM


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>
  

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 05 Mar 2012, 02:46 PM
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 >>
Tags
Book
Asked by
Eric
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or