Accessing elements inside DataTemplate from code behind in WPF

Hi I would like to access the controls inside a datatemplate in the xaml file from the code behind and assign some properties.. PLease let me know how to do that
<DataTemplate x:Key="SlideShowTemplate" DataType="{x:Type loc:MainContent}">
            <Grid Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FF000000">
                    <ColumnDefinition Width="*"/>
                <Rectangle x:Name="rec1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                <Rectangle x:Name="rec2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

Open in new window

Who is Participating?
unmeshdaveConnect With a Mentor Commented:
Author did not comment on the solution provided. without any conversion no body can give solution. even he did not mention that he tried the solution provided or not.
u need to first get the datatemplate reference. you can get it by implementing DataTemplateSelector class. Override this class & implement SelectTemplate Method.  
after you can use LoadContent Method of DataTemlate to access child.  
well my choice will be
4) Question answered by one or more Expert comments
comment ID:  24396553.
The spicified article link clearly solves the problem mentioned by author. It clearly mentions how to access elements inside datatemplate from code behind.
ctrlctrlAuthor Commented:
Hi All, Sorry about that , Let me review again before requesting a delete.

Thank You
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.