Accessing ComboBox in DataTemplate from CodeBehind

7 posts, 0 answers
  1. Andreas
    Andreas avatar
    23 posts
    Member since:
    Feb 2011

    Posted 18 Mar 2011 Link to this post

    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
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 18 Mar 2011 Link to this post

    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
  3. DevCraft banner
  4. Ken
    Ken avatar
    6 posts
    Member since:
    Dec 2010

    Posted 21 Mar 2011 Link to this post

    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.

  5. Andreas
    Andreas avatar
    23 posts
    Member since:
    Feb 2011

    Posted 21 Mar 2011 Link to this post

    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
  6. Joao Paulo Grassi
    Joao Paulo Grassi avatar
    98 posts
    Member since:
    Feb 2011

    Posted 20 Oct 2011 Link to this post

    I created a topic just now, i have the same issue.
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 21 Oct 2011 Link to this post

    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 >>

  8. Joao Paulo Grassi
    Joao Paulo Grassi avatar
    98 posts
    Member since:
    Feb 2011

    Posted 21 Oct 2011 Link to this post

    Hi Maya,
    Thank you, your explanation was very helpful.


Back to Top
DevCraft banner