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

[Solved] SelectionChangedCommand and button click events in RadGridView with ChildTableDefinitions

1 Answer 485 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Justin D.
Top achievements
Rank 1
Justin D. asked on 13 Jan 2011, 03:28 PM

I erroneously posted this to the General Discussion section yesterday. This post clearly belongs here since it relates to the RadGridView.

I am using the RadGridView with two child tables. In the code below, I have stripped out my column names and have renamed my entity names to Level1, Level2, and Level3 for clarity.

Each grid has its own selection changed event. When I click on a row in the parent grid, I expect the Level1SelectionChangedCommand to be called in my View Model. When I click on a row in the first child grid (second level), I expect the Level2SelectionChangedCommand to be called in my View Model. When I click on a row in the third level, I expect the Level3SelectionChangedCommand to be called in my ViewModel. However, that is not happening. No matter what row in what grid/level I click on, the Level1SelectionChangedCommand is always what is being called. This even happens if I click on a row in my Level2Grid or my Level3Grid.

How do I make the correct function be called depending on what grid I click on? I am using RadControls for Silverlight Q3 2010. Thanks in advance for the help.

UPDATE: Since yesterday I have tried adding buttons above each level's grid (reflected in code sample below.) The buttons above the Level1Grid fire their events just fine. But the buttons above the Level2 and Level3 grid do not fire at all. What is the cause of this?

<StackPanel Orientation="Vertical"
            Margin="0,10,0,0"
            HorizontalAlignment="Center">
      
    <StackPanel Orientation="Horizontal"
                VerticalAlignment="Center">
        <telerik:RadButton Content="Add new level1"
                           x:Name="AddLevel1Button"
                           Margin="10,0,0,0"
                           Command="{Binding Level1AddCommand}"
                           CommandParameter="{Binding ElementName=Level1Grid}"
                           VerticalAlignment="Center"/>
    </StackPanel>
    <telerik:RadGridView x:Name="Level1Grid"
                         CanUserFreezeColumns="False" 
                         GridLinesVisibility="Horizontal" 
                         IsFilteringAllowed="False"
                         ShowGroupPanel="False"
                         ItemsSource="{Binding Level1, Mode=TwoWay}"  
                         IsReadOnly="False" 
                         Width="1000"
                         Margin="0,10,0,0"
                         AutoGenerateColumns="False">
        <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition />
        </telerik:RadGridView.ChildTableDefinitions>
        <telerik:RadGridView.Columns>
          <!-- columns here -->
        </telerik:RadGridView.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
               <i:InvokeCommandAction Command="{Binding Level1SelectionChangedCommand}" CommandParameter="{Binding ElementName=Level1Grid, Mode=OneWay}"></i:InvokeCommandAction>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal"
                                VerticalAlignment="Center">
                        <telerik:RadButton Content="Add new level2"
                                           x:Name="AddLevel2Button"
                                           Margin="10,0,0,0"
                                           Command="{Binding Level2AddCommand}"
                                           CommandParameter="{Binding ElementName=Level2Grid}"
                                           VerticalAlignment="Center"/>
                    </StackPanel>
                      
                    <telerik:RadGridView x:Name="Level2Grid" 
                                         CanUserFreezeColumns="False" 
                                         GridLinesVisibility="Horizontal" 
                                         IsFilteringAllowed="False"
                                         ShowGroupPanel="False"
                                         ItemsSource="{Binding Level2, Mode=TwoWay}"  
                                         IsReadOnly="False" 
                                         Width="800"
                                         Margin="0,10,0,0"
                                         AutoGenerateColumns="False">
                        <telerik:RadGridView.ChildTableDefinitions>
                            <telerik:GridViewTableDefinition />
                        </telerik:RadGridView.ChildTableDefinitions>
                        <telerik:RadGridView.Columns>
                          <!-- columns here -->
                        </telerik:RadGridView.Columns>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectionChanged">
                                <i:InvokeCommandAction Command="{Binding Level2SelectionChangedCommand}" CommandParameter="{Binding ElementName=Level2Grid, Mode=OneWay}"></i:InvokeCommandAction>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <telerik:RadGridView.HierarchyChildTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical"
                                            HorizontalAlignment="Center">
                                    <StackPanel Orientation="Horizontal"
                                                VerticalAlignment="Center">
                                        <telerik:RadButton Content="Add new level3"
                                                           x:Name="AddLevel3Button"
                                                           Margin="10,0,0,0"
                                                           Command="{Binding Level3AddCommand}"
                                                           CommandParameter="{Binding ElementName=Level3Grid}"
                                                           VerticalAlignment="Center"/>
                                    </StackPanel>
                                    <telerik:RadGridView x:Name="Level3Grid"
                                                         CanUserFreezeColumns="False" 
                                                         GridLinesVisibility="Horizontal" 
                                                         IsFilteringAllowed="False"
                                                         ShowGroupPanel="False"
                                                         ItemsSource="{Binding Level3, Mode=TwoWay}"
                                                         IsReadOnly="False" 
                                                         Width="600"
                                                         Margin="0,10,0,0"
                                                         AutoGenerateColumns="False">
                                        <telerik:RadGridView.Columns>
                                            <!-- column definitions here -->
                                        </telerik:RadGridView.Columns>
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="SelectionChanged">
                                                <i:InvokeCommandAction Command="{Binding Level3SelectionChangedCommand}" CommandParameter="{Binding ElementName=Level3Grid, Mode=OneWay}"></i:InvokeCommandAction>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </telerik:RadGridView>
                                </StackPanel>
                            </DataTemplate>
                        </telerik:RadGridView.HierarchyChildTemplate>
                    </telerik:RadGridView>
                </StackPanel>
            </DataTemplate>
        </telerik:RadGridView.HierarchyChildTemplate>
    </telerik:RadGridView>
</StackPanel>

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 18 Jan 2011, 01:17 PM
Hello Justin D.,

Basically, the reason for this behavior is that the SelectionChanged for the parent grid is fired also when a selection is made in the children grids. What you can do is to handle the SelectionChanged event of the parent grid and invoke the corresponding commands depending on the grid you have clicked on:

private void Level1Grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (e.OriginalSource == this.Level1Grid)
            {
                model.Level1SelectionChangedCommand.Execute(null);
            }
            else
            {
                model.Level2SelectionChangedCommand.Execute(null);
            }
        }

In this case model is the instance of your ViewModel.
 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Justin D.
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or