Add context menu in datagrid, how to get the select Item value

后端 未结 5 1229
说谎
说谎 2020-12-03 20:20

I am new in WPF programming with MVVM pattern. Now I have added the context menu in the datagrid. But, when I click the right mouse button, I don\'t know how to get the sele

5条回答
  •  离开以前
    2020-12-03 20:37

    bigworld12 has close to the right answer here, but it breaks if your context menu is templated. Try:

    DataGridRow row =
                ((sender as MenuItem)?.GetAncestors()
                   ?.FirstOrDefault(dpo => dpo.GetType() == typeof(ContextMenu)) as ContextMenu)
                   ?.PlacementTarget as DataGridRow;
    

    for the code-behind. I used nullable operators in case you somehow get here without the expected parent tree and target (maybe by triggering the context menu while off the grid.)

提交回复
热议问题