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

Accessing ComboBox in DataTemplate from CodeBehind

6 Answers 354 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 18 Mar 2011, 11:16 AM
Hi,

I've put four ComboBoxes in different Columns of a RadGridView. Now I would like to access the ComboBoxes from the CodeBehind but I get the error that they are not available in this context. How I can access them? My XAML looks like this:

<t1:GridViewDataColumn Header="Level1" DataMemberBinding="{Binding tbl_1}" >
                    <t:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Name="Level1" .../>

CodeBehind
Level1.SelectedIndex -> compile error: Level1 is not available in this context

Regards,

Andreas

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 18 Mar 2011, 11:23 AM
Hello Andreas,

You cannot access the element defined in a DataTemplate in such a manner. What you may try is to benefit from the ChildrenOfType<T> extension method:

var row = this.clubsGrid.ItemContainerGenerator.ContainerFromIndex(1) as GridViewRow;
var combo = row.ChildrenOfType<ComboBox>();

In this case you will get the ComboBox on the second row.

All the best,
Maya
the Telerik team
0
Ken
Top achievements
Rank 1
answered on 21 Mar 2011, 02:37 PM
Something I've done in the past is to create a Loaded handler for the combobox in the template.
When that event fires (for each combobox) I add the combobox to a Dictionary where the key
is whatever I need (unique id for row, itself; the row number; etc...).

Then when I need to access the combobox in the future I just get it from the dictionary using the
appropriate key.

0
Andreas
Top achievements
Rank 1
answered on 21 Mar 2011, 04:12 PM
Thanks for your suggestions they were really helpful but now I have changed my scenario. I have still four ComboBoxes nested in the GirdView. In my VM there is one ObservabelCollection for the GridView and four ObservableCollections for the ComboBoxes. My problem is that the ComboBoxes inherit the ItemSource from the GridView what is not what I want. How can I bind the ComboBoxes to a different ObservableCollection from thesame VM. Here is what I have tried so far:

<t1:RadGridView ItemsSource="{Binding Liste}"  SelectedItem="{Binding Current, Mode=TwoWay}" AutoGenerateColumns="False">
    <
            <t1:RadGridView.Columns>
                <t1:GridViewDataColumn>
                    <t:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <t:RadComboBox Width="150" Margin="5"
                                DataContext="{Binding Path=LimkVM}"
                                ItemsSource="{Binding Path=EigenschaftenEbene1, Mode=TwoWay}"
                                SelectedValue="{Binding Path=AusgewaehlteEigenschaft1, Mode=TwoWay}"
                                ...

Thanks for any suggestion,

Andreas
0
Missing User
answered on 20 Oct 2011, 07:47 PM
I created a topic just now, i have the same issue.
0
Maya
Telerik team
answered on 21 Oct 2011, 07:27 AM
Hi,

@Andreas:
You can bind the ItemsSource of each combo box in the CellTemplate to different properties in your ViewModel as follows:

                <t1:GridViewDataColumn>
                    <t:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <t:RadComboBox Width="150" Margin="5"
                                ItemsSource="{Binding Path=EigenschaftenEbene1, Source="{StaticResource MyViewModel}"}"
                                SelectedValue="{Binding Path=AusgewaehlteEigenschaft1, Mode=TwoWay}" />
                       </DataTemplate>
                     <t:GridViewDataColumn.CellTemplate>
             <t1:GridViewDataColumn>
<t1:GridViewDataColumn>
                    <t:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <t:RadComboBox Width="150" Margin="5"
                                ItemsSource="{Binding Path=EigenschaftenEbene2, Source="{StaticResource MyViewModel}"}"
                                SelectedValue="{Binding Path=AusgewaehlteEigenschaft2, Mode=TwoWay}" />
                       </DataTemplate>
                     <t:GridViewDataColumn.CellTemplate>
             <t1:GridViewDataColumn>

@ Joao:
I have just replied to your forum thread.

Greetings,
Maya
the Telerik team

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

0
Missing User
answered on 21 Oct 2011, 11:29 AM
Hi Maya,
Thank you, your explanation was very helpful.


Tags
GridView
Asked by
Andreas
Top achievements
Rank 1
Answers by
Maya
Telerik team
Ken
Top achievements
Rank 1
Andreas
Top achievements
Rank 1
Missing User
Share this question
or