Solved

Accessing elements inside DataTemplate  from code behind in WPF

Posted on 2009-05-12
6
3,594 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

9 Experts available now in Live!

Get 1:1 Help Now