wpf的datagrid获取当前内容
时间: 2023-10-10 10:08:54 浏览: 79
要获取 WPF DataGrid 中当前行的内容,可以使用以下代码:
```csharp
// 获取当前选中的行
var selectedRow = myDataGrid.SelectedItem;
// 获取选中行的数据对象
var dataObject = selectedRow as MyDataObject;
// 获取选中行的某一列的值
var columnValue = dataObject.ColumnName;
```
其中,`myDataGrid` 是你的 DataGrid 控件的名称,`MyDataObject` 是你绑定到 DataGrid 的数据对象类型,`ColumnName` 是你想要获取的列的名称。
相关问题
WPF datagrid获取当前选中的单元格
可以使用以下代码获取当前选中的单元格:
```
DataGridCellInfo cellInfo = myDataGrid.SelectedCells[0];
object item = cellInfo.Item;
string propertyValue = cellInfo.Column.GetCellContent(item).ToString();
```
其中,myDataGrid为DataGrid控件的名称,可以根据实际情况进行修改。
WPF datagrid获取当前选中的行和列
您可以使用以下代码获取WPF DataGrid中当前选中的行和列:
```csharp
private void yourDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (yourDataGrid.SelectedItem != null)
{
int row = yourDataGrid.SelectedIndex;
int column = yourDataGrid.CurrentColumn.DisplayIndex;
//执行您需要的逻辑
}
}
```
请注意,您需要在XAML文件中向DataGrid添加SelectionChanged事件处理程序(在这个示例中是yourDataGrid_SelectionChanged)。
希望这可以帮助您。