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

Commanding with Hyperlink button / selecteditem

3 Answers 233 Views
GridView
This is a migrated thread and some comments may be shown as answers.
bob
Top achievements
Rank 1
bob asked on 17 Mar 2011, 06:41 PM
I initially created the project with a regualr datagrid. When the user clicks on a hyperlink button from the datagrid 2 commands are fired. One for setting the view model's selecteditem and then the command to load the correct details based on which button was clicked and which selecteditem is set. This does not work with the radgridview. If you just click on the hyperlinkbutton is only calls the command for the button and does not fire the selecteditem command at all which is bound to the selectionChanged event of the radgrid.

I have a couple of questions here about the gridview. How can I use the gridview and get both parameters sent to the viewmodel so I can load the correct data?

Here's what I currently have... if there's another way to do this please let me know.

[code]
<telerik:RadGridView AutoGenerateColumns="False"
                        ItemsSource="{Binding DiscAggregates}"
                        SelectedItem="{Binding SelectedSchool,Mode=TwoWay}" Margin="0,75,0,0" Name="rgvDiscipline" SelectionMode="Single" SelectionUnit="FullRow">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <cmd:EventToCommand
                Command="{Binding SelectionChangedCommand}"
                CommandParameter="{Binding SelectedItem, ElementName=dgDisc}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn DataMemberBinding="{Binding InSchool}" Header="In School" IsReadOnly="True">
            <telerik:GridViewDataColumn.CellTemplate>
                <DataTemplate>
                    <HyperlinkButton
                        Content="{Binding InSchool}"
                        Command="{Binding Source={StaticResource Locator},Path=Discipline.SchoolDetailsCommand}"
                        CommandParameter="InSchool"
                        NavigateUri="/Views/details.xaml"
                        HorizontalAlignment="Left" Name="HyperlinkButton1" VerticalAlignment="Top" />
                </DataTemplate>
            </telerik:GridViewDataColumn.CellTemplate>
        </telerik:GridViewDataColumn>
[/code]

3 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 18 Mar 2011, 05:31 PM
Hi bob,

 
I believe that in your case the better option would be to expose a SelectedItem property of the ViewModel, which you can pass as a CommandParameter to the HyperlinkButton.
You may find attached sample project that demonstrates this approach.Please let me know how this works for you.

All the best,
Vanya Pavlova
the Telerik team
0
bob
Top achievements
Rank 1
answered on 18 Mar 2011, 05:39 PM
I came up with a work around, which was to have multiple commands one for each hyperlink column and passed datacontext of the button to the command. Which is probably what your example code suggests to do. This is rather clumsy approach to solving this issue, when clicking on an object inside a row should trigger a SelectedItem change on the grid. Is this a known issue?
0
Vanya Pavlova
Telerik team
answered on 21 Mar 2011, 08:17 AM
Hello Bob,

 
Actually, the SelectionChanged event of RadGridView is fired when the selected GridViewRow has changed. A grid row can be selected when its IsSelected property is set to true, when its corresponding data item is added to the SelectedItems collection or when its correspoding data item is set as SelectedItem. Your code has to perform one of those actions to mark an item/row as selected. In the example from my previous post was demonstrated the second approach. When the HyperlinkButton is clicked, the custom command is being executed and using the provided CommandParameter you take selected row out. 

If you need any additional information, please let me know.


All the best,
Vanya Pavlova
the Telerik team
Tags
GridView
Asked by
bob
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
bob
Top achievements
Rank 1
Share this question
or