<Employees>
<Employee>
<EmpId>1</EmpId>
<Name>Sam</Name>
<Sex>Male</Sex>
</Employee>
<Employee>
<EmpId>2</EmpId>
<Name>Lucy</Name>
<Sex>Female</Sex>
</Employee>
</Employees>
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="0.8*"/>
<RowDefinition Height="0.05*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="DgrdEmployeeDetails" Grid.Row="0" FontSize="14.667" FontWeight="Bold" RowHeight="60" ColumnHeaderHeight="60" RowHeaderWidth="40" ColumnWidth="*"/>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<Button x:Name="BtnGetSelectedRow" Content="GetSelectedRow" Width="120" Height="50" Click="BtnGetSelectedRow_Click" />
</StackPanel>
</Grid>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
XDocument emplyeeDetails = XDocument.Load("Employees.xml");
var emplyees = from emp in emplyeeDetails.Descendants("Employee").Take(10)
orderby emp.Element("EmpId").Value ascending
select new
{
Id = emp.Element("EmpId").Value,
Name = emp.Element("Name").Value,
Sex = emp.Element("Sex").Value
};
DgrdEmployeeDetails.ItemsSource = emplyees.ToList();
}
private void BtnGetSelectedRow_Click(object sender, RoutedEventArgs e)
{
DataRowView dr= DgrdEmployeeDetails.SelectedItem as DataRowView;
}
当我单击 GetSelectedRow按钮时,博士返回空值