获取GridView的Clicked Row

时间:2016-02-02 05:52:13

标签: c# silverlight row telerik-grid

每行都附加RadGridViewHyperLink按钮。我想在Hyperlink的ID事件中获取列Click的值。我试过这个,但得到NullReferenceException,因为SelectedItem包含null。

private void OnGetDetailsClick(object sender, RoutedEventArgs e)
{
   ProductDetail productDetail = new ProductDetail();
   Product product = this.grdProductGrid.SelectedItem as Product;
   productDetail.ProductId = product.ProductID
}

RadGridView

的Xaml
<telerik:RadGridView HorizontalAlignment="Left" 
                          Height="278" 
                          VerticalAlignment="Top" 
                          Width="1220" 
                          Margin="20,0,0,0" 
                          Name="grdProductGrid"
                          ColumnWidth="*"
                          AutoGenerateColumns="False" >

                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn x:Name="ID" 
                                                DataMemberBinding="{Binding Path=ProductID}" 
                                                Header="ID" 
                                                IsReadOnly="True" 
                                                IsVisible="False"/>


                    <telerik:GridViewDataColumn x:Name="Name" 
                                                DataMemberBinding="{Binding Path=Name}" 
                                                Header="Name" 
                                                IsReadOnly="True" />

                    <telerik:GridViewDataColumn x:Name="Description" 
                                                DataMemberBinding="{Binding Path=Description}" 
                                                Header="Description" 
                                                IsReadOnly="True" />


                    <telerik:GridViewColumn Header="Details" Width="*">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <HyperlinkButton Content="Details" 
                                                     Tag="Hyperlinkbutton" 
                                                     HorizontalAlignment="Center" 
                                                     Click="OnGetDetailsClick"/> 
                                </StackPanel>
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewColumn>
                  </telerik:RadGridView.Columns>
</telerik:RadGridView>

我的代码有什么问题?如何访问Click事件的行?

1 个答案:

答案 0 :(得分:2)

您可以使用CommandParameterProduct传递给点击事件

  <HyperlinkButton Content="Details" 
    Tag="Hyperlinkbutton" 
    HorizontalAlignment="Center" 
   Click="OnGetDetailsClick" CommandParameter={Binding .}/> 

并且在您的代码后面有

Product product=((Product)((HyperlinkButton)sender).CommandParameter)