2012-05-22 51 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