wpf - 获取 DataGrid WPF 中选定的行项目

标签 wpf datagrid selecteditem

我有一个DataGrid,绑定(bind)到数据库表,我需要获取DataGrid中选定行的内容,例如我想在中显示MessageBox 所选行的内容。

DataGrid 示例:

enter image description here

因此,如果我选择第二行,我的 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/

相关文章:

c# - WPF:使用匿名方法的 UnauthorizedAccessException

wpf - DataGrid:关于单元格验证错误,其他行单元格不可编辑/只读

wpf - 如何使用 List(Of T) 作为 WPF DataGrid 的 Itemssource?

mvvm - 使用 MVVM 模式在 Telerik RadGridView 中查找选定项目

c# - MVVM,何时使用 IValueConverter 以及何时使用 "formatted"属性

c# - 包含 MaterialDesignInXaml 后无法使用 TextBox

c# - 在 silverlight 和 WPF 中实现多语言应用程序的最佳方式

WPF 数据网格性能

c# - 为什么 DropDownList.SelectedItem.Value 不显示赋值

c# - 取消装箱卡住 Winforms ComboBox