This is a migrated thread and some comments may be shown as answers.

Code behind access to controls inside a DataTemplate?

3 Answers 397 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Rob Ainscough
Top achievements
Rank 1
Rob Ainscough asked on 14 Nov 2011, 11:33 PM

I'm using the RadGridView - RowsDetailsTemplate - where I have a DataTemplate setup.

Having NO LUCK trying to reference/access controls I have in my DataTemplate from code behind (all the controls do have x:Name=). I want to be able to handle a Click event on a button I have in my DataTemplate ... can't find anyway to do this. (VB side).

I've read several similar questions but no one actually posted a solution or sample code.

Some suggestions so far:

1. Setup a class to handle the event and bind to that class -- don't really want to setup an entire class just to handle a simple click event from one control.

2. Another suggest was load the DataTemplate and "Find" the control and then add a handler for the control -- again seems like a lot of work to and I'm guessing pretty slow performance wise.

I'm relatively new to Silverlight 4 so there is a good possibility I'm missing something obvious, but how is one supposed to access DataTemplate controls and events from code behind?

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 15 Nov 2011, 07:45 AM
Hi Rob Ainscough,

Indeed, when a control is placed inside a DataTemplate, you cannot reach it through its Name attribute. What you can try is to define the event in xaml and handle it directly in code-behind:

XAML:
<telerik:RadGridView.RowDetailsTemplate>
                <DataTemplate>
                    <telerik:RadButton Content="MyButton" Click="RadButton_Click" />
                </DataTemplate>
            </telerik:RadGridView.RowDetailsTemplate>
 
Code-behind:
private void RadButton_Click(object sender, RoutedEventArgs e)
        {
 
        }

Will that approach fits into your scenario ? 
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Rob Ainscough
Top achievements
Rank 1
answered on 15 Nov 2011, 05:40 PM
Excellent, that did the trick by adding the Click="event".

But I'm still somewhat confused on DataTemplate usage.  The Telerik online examples for RadGridView uses DataTemplate for the RowDetailsTemplate ... as far as I know there is no other way to use RowDetailsTemplate other than specifying a DataTemplate?

So how exactly am I going to access the other controls in my DataTemplate (beyond just responding to a Button click event) to be populated when I expand RowDetails from the parent RadGridView?

Sorry if the question is Noobish in nature.
0
Maya
Telerik team
answered on 15 Nov 2011, 06:43 PM
Hi Rob Ainscough,

Indeed, you have to define DataTemplate inside the RowDetails. I will recommend you to run through our online documentation for additional reference. You can also take a look at the LoadingRowDetails event and examine the DetailsElement - this is the one containing all elements in row details.
Furthermore, you can get those elements from the corresponding row. You can read this blog post for a reference on how to get child and parent visual items.
 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Maya
Telerik team
Rob Ainscough
Top achievements
Rank 1
Share this question
or