Solved

Accessing elements inside DataTemplate  from code behind in WPF

Posted on 2009-05-12
6
3,614 Views
Last Modified: 2013-12-17
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">

                <Grid.ColumnDefinitions>

                    <ColumnDefinition Width="*"/>

                </Grid.ColumnDefinitions>

                <Rectangle x:Name="rec1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

                <Rectangle x:Name="rec2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

            </Grid>

        </DataTemplate>

Open in new window

0
Comment
Question by:ctrlctrl
  • 3
6 Comments
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24396553
u need to first get the datatemplate reference. you can get it by implementing DataTemplateSelector class. Override this class & implement SelectTemplate Method.
http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.selecttemplate.aspx  
after you can use LoadContent Method of DataTemlate to access child.
http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.loadcontent.aspx  
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 500 total points
ID: 24650593
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.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24660059
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.
0
 

Author Comment

by:ctrlctrl
ID: 24661598
Hi All, Sorry about that , Let me review again before requesting a delete.

Thank You
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now