我有一个DataGrid
,绑定(bind)到数据库表,我需要获取DataGrid
中选定行的内容,例如我想在中显示MessageBox
所选行的内容。
DataGrid
示例:
因此,如果我选择第二行,我的 MessageBox
必须显示类似以下内容:646 Jim Biology。
最佳答案
您可以使用SelectedItem
属性来获取当前选定的对象,然后可以将其转换为正确的类型。例如,如果您的 DataGrid
绑定(bind)到 Customer
对象的集合,您可以执行以下操作:
Customer customer = (Customer)myDataGrid.SelectedItem;
或者,您可以将 SelectedItem
绑定(bind)到源类或 ViewModel
。
<Grid DataContext="MyViewModel">
<DataGrid ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>
</Grid>
关于wpf - 获取 DataGrid WPF 中选定的行项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3913580/