Ho to get the selected item?

3 posts, 0 answers
  1. Richard
    Richard avatar
    11 posts
    Member since:
    Mar 2011

    Posted 26 Dec 2011 Link to this post

    Hi

    I have a grid view like this:

    <telerik:RadGridView    x:Name="GridView1"                            
                            AutoGenerateColumns="False"
                            ItemsSource="{Binding ListeMfp}" SelectionChanged="GridView1_SelectionChanged">
     
        <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition />
        </telerik:RadGridView.ChildTableDefinitions>
         
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Nom}"
                                   Header="Nom " />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Emplacement}"
                                   Header="Emplacement" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Constructeur}"
                                   Header="Constructeur" />
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Modele}"
                                   Header="Modèle" />
        </telerik:RadGridView.Columns>
     
        <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
                <telerik:RadGridView
                    x:Name="GridView2"
                    ShowGroupPanel="False"
                    ItemsSource="{Binding Filtres}"
                    AutoGenerateColumns="False" SelectionChanged="Filtres_SelectionChanged">
                     
                    <telerik:RadGridView.ChildTableDefinitions>
                        <telerik:GridViewTableDefinition />
                    </telerik:RadGridView.ChildTableDefinitions>
                     
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn
                            Header="Nom du filtre"
                            DataMemberBinding="{Binding Nom}"/>
                        <telerik:GridViewDataColumn
                            Header="Description du filtre"
                            DataMemberBinding="{Binding DescriptionFiltre}"/>
                        <telerik:GridViewDataColumn
                            Header="Nombre de sous-filtres"
                            DataMemberBinding="{Binding NombreSousFiltres}"/>
                    </telerik:RadGridView.Columns>
            </DataTemplate>
        </telerik:RadGridView.HierarchyChildTemplate>
    </telerik:RadGridView>

    Basicaly, a gridview (GridView1) showing items who have sub-items (in GridView2). Please, how do I get the selected item from GridView2?

    While the following code is ok:
    private void GridView1_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
    {
        _Item = GridView1.SelectedItem as ItemObject; 
    }


    this one will not compile, as GridView2 is unknown:
    private void GridView2_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
    {
        _subItem = GridView2.SelectedItem as SubItem;
    }

    Please, how can I get the subtem?

    Thanks for your help.

    Richard
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Dec 2011 Link to this post

    Hello Richard,

    Generally, you cannot get a visual element defined in DataTemplate through its 'Name' attribute. Even if that was possible, which instance of the child grids you would get as there is one for each parent item. What you can try is to get the child grid (through ChildrenOfType<T> extension method) and find the SelectedItem. Another possible way to go would be to bind the SelectedItem of the inner grid to a property from your ViewModel for example. However, the most correct approach depends entirely on your specific scenario.  


    Greetings,
    Maya
    the Telerik team

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

  3. Richard
    Richard avatar
    11 posts
    Member since:
    Mar 2011

    Posted 27 Dec 2011 Link to this post

    Thank you for your answer, Maya!
Back to Top