【发布时间】:2025-11-28 04:00:02
【问题描述】:
我尝试做这样的事情:
<DataGrid Name="myGrid" ItemSource="{Binding Path=MyCollection}">
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem
Command="{Binding RemoveRow}"
CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}">
</ContextMenu>
</DataGridContextMenu>
</DataGrid>
但我总是得到空值(我也尝试过 SelectedIndex 和 SelectedValue)
如果我将以下参数传递给执行代码,它会起作用:
<MenuItem Command="{Binding RemoveRow}" CommandParameter="1">
【问题讨论】:
-
您确认 SelectedItem 不为空吗?
-
我不知道这是否适合你,但你可以检查 datagrid SelectedItem 属性。
标签: wpf xaml binding mvvm icommand