Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accessing elements inside DataTemplate  from code behind in WPF

Posted on 2009-05-12
6
Medium Priority
?
3,808 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 1500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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