WPF DataGrid 获取选定的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19455633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:54:06  来源:igfitidea点击:

WPF DataGrid get selected row

wpfwpfdatagrid

提问by

Need to do some logic based on the current row selected. New to WPF before I would do something like this;

需要根据当前选中的行做一些逻辑。在我做这样的事情之前是 WPF 的新手;

int i myDataGridView.CurrentCell.RowIndex

Now that is not available, what is best way to get selected row?

现在不可用,获取选定行的最佳方法是什么?

采纳答案by Nitin

Use DataGrid.SelectedItemproperty. It gives the Selected Item on the DataGrid if SelectionModeis Single. Else DataGrid.SelectedItemsgives the multiple selection if SelectionMode is Extendedor Multiple

使用DataGrid.SelectedItem财产。如果SelectionMode是 ,它会给出 DataGrid 上的 Selected Item SingleDataGrid.SelectedItems如果 SelectionMode 是Extended或 Multiple,则Else给出多项选择

回答by Josh

Try this:

尝试这个:

SomeObject someObject = (SomeObject)myDataGridView.SelectedItem

SomeObject someObject = (SomeObject)myDataGridView.SelectedItem