Solved

Accessing elements inside DataTemplate  from code behind in WPF

Posted on 2009-05-12
6
3,723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

726 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