每行都附加RadGridView
和HyperLink
按钮。我想在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
<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
事件的行?
答案 0 :(得分:2)
您可以使用CommandParameter
将Product
传递给点击事件
<HyperlinkButton Content="Details"
Tag="Hyperlinkbutton"
HorizontalAlignment="Center"
Click="OnGetDetailsClick" CommandParameter={Binding .}/>
并且在您的代码后面有
Product product=((Product)((HyperlinkButton)sender).CommandParameter)