我有一个DataGrid
,每行有ID,Name
列和HyperLinkButton
。我的ID
列被属性Visibility="Collapsed"
隐藏。在HyperLinkButton的Click
事件中,我正在访问我的列值,但是当它被隐藏时我无法访问该列,即它返回null。它只有在我Visible
时才能访问。有没有解决这个问题的方法?
DataGrid的XAML:
<sdk:DataGrid AutoGenerateColumns="False"
HorizontalAlignment="Left"
Height="163"
VerticalAlignment="Top"
Name="ProductGrid">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn x:Name="ID"
Binding="{Binding Path=ProductID, Mode=OneWay}"
Header="ID"
IsReadOnly="True"
Width="50"
Visibility="Collapsed" />
<sdk:DataGridTextColumn x:Name="Name"
Binding="{Binding Path=Name, Mode=OneWay}"
Header="Name"
IsReadOnly="True"
Width="340" />
<sdk:DataGridTemplateColumn Header="">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="gridTemplate">
<StackPanel Orientation="Vertical"
VerticalAlignment="Center">
<HyperlinkButton Content="Details"
Tag="Hyperlinkbutton"
HorizontalAlignment="Center"
Click="OnGetDetailsClick"
Width="100" />
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
HyperLink的点击事件:
var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
TextBlock txtblkID = (TextBlock)ProductGrid.Columns[0].GetCellContent(selectedGridRow);
p.ID = Int32.Parse(txtblkID.Text);
答案 0 :(得分:1)
您可以获取所选行的DataContext并从中获取值。用您的模型替换DataSourceType强制转换。
var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
p.ID = ((DataSourceType)selectedGridRow.DataContext).ProductID;