2012-05-22 48 views
1

我有一个ListViewGridView在其中的WPF应用程序。当您右键单击网格中的项目时,它有一个ContextMenu。我想知道如何访问从ContextMenu中选择的行并以编程方式访问。我的目标是删除该行数据。谢谢!WPF ContextMenu访问所选行

回答

2

这将工作用WPF command bindings ...

<ListView> 
     <!-- .... --> 
     <ListView.ContextMenu> 
       <ContextMenu> 
        <MenuItem Header="Remove Item" Command="{Binding RemoveItem}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},Path=PlacementTarget.SelectedItem}" Icon="{StaticResource deleteIcon}"/> 
      </DataGrid.ContextMenu> 
     </ListView.ContextMenu> 
    </ListView> 

要创建自定义命令绑定,看到this SO post