Accessing items in ContentTemplate from outside the control

2 posts, 1 answers
  1. Answer
    Darren
    Darren avatar
    6 posts
    Member since:
    Dec 2010

    Posted 24 Jan 2011 Link to this post

    I have a tab control that has its ItemsSource bound to an EntityCollection and the ContentTemplate set to a DataTemplate.

    DataTemplate:
    <DataTemplate x:Key="ContentTemplate">
         <Grid x:Name="TemplateGrd">
            <StackPanel x:Name="panelSub" >
                <TextBlock Text="" Name="text1" />
                <TextBlock Text="{Binding dgpGroupDesc}" Name="text2"></TextBlock>
           </StackPanel>
        </Grid>
    </DataTemplate>

    TabControl:

    <telerikNavigation:RadTabControl Height="660" Width="975" Name="tabsVMB"
                                   TabOrientation="Horizontal"  TabStripPlacement="Right"
                                   ItemTemplate="{StaticResource TabItemTemplate}"
                                   ContentTemplate="{StaticResource ContentTemplate}"
                                   ItemsSource="{Binding ElementName=DomainDataSource, Path=Data}" />

    In the Loaded_Data event handler for the DomainDataSource, I need to set the Text in the "text1" TextBlock. How can I traverse from the TabControl object down to that control? I'm still really new to Silverlight and this has been giving me headaches for a couple of days. Any help?
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 27 Jan 2011 Link to this post

    Hello Darren,

    In order to access the "text1" TextBlock element, the RadTabControl needs to be loaded and initialized. Then you can walk through the visual tree and look for this TextBlock:
    foreach (TextBlock item in tabsVMB.ChildrenOfType<TextBlock>())
    {
        if (item.Name == "text1")
        {
            item.Text = "Text";
        }
    }
    However, on the DomainDataSource LoadedData() event the RadTabControl is not yet initialized so that approach cannot be applied.

    Another approach is to define a Loaded() event for the TextBlock and handle it to define the TextBlock Text property in code behind:
    <DataTemplate>
        <StackPanel x:Name="panelSub">
            <TextBlock Text=""
                 Name="text1"
                                                      Loaded="text1_Loaded" />
            <TextBlock Text="{Binding CompanyName}"
                        Name="text2"></TextBlock>
        </StackPanel>
    </DataTemplate>

    Still, the data binding approach is much better and when you bind your data in XAML, it is not a good approach to set properties in code as well. So if you can elaborate on your scenario and what exactly you need to display in the Text property, we might be able to suggest a better approach. For example, you can use an IValueConverter to convert a binded property.

    Greetings,
    Tina Stancheva
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
Back to Top