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

Ho to get the selected item?

2 Answers 31 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 26 Dec 2011, 05:29 PM
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 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Dec 2011, 08:14 AM
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 >>

0
Richard
Top achievements
Rank 1
answered on 27 Dec 2011, 02:30 PM
Thank you for your answer, Maya!
Tags
GridView
Asked by
Richard
Top achievements
Rank 1
Answers by
Maya
Telerik team
Richard
Top achievements
Rank 1
Share this question
or