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
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
0
Hello Andreas,
In this case you will get the ComboBox on the second row.
All the best,
Maya
the Telerik team
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.
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
<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
Hi,
@ Joao:
I have just replied to your forum thread.
Greetings,
Maya
the Telerik team
@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