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

Accessing items in ContentTemplate from outside the control

1 Answer 82 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Darren
Top achievements
Rank 1
Darren asked on 24 Jan 2011, 09:44 PM
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?

1 Answer, 1 is accepted

Sort by
0
Tina Stancheva
Telerik team
answered on 27 Jan 2011, 03:30 PM
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>>
Tags
TabControl
Asked by
Darren
Top achievements
Rank 1
Answers by
Tina Stancheva
Telerik team
Share this question
or