通过单击列表框中的整行打开上下文菜单

Open context menu by click on the whole line in listbox

在我的代码中我有:

    <ListBox Name="Playlists_ListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2" >
                    <Grid.ContextMenu>
                        <ContextMenu Name="cm"  StaysOpen="true" >
                            <MenuItem Header="Delete"/>
                        </ContextMenu>
                    </Grid.ContextMenu>
                    <TextBlock Name="Name" Text="{Binding Title}"  Foreground="White"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

当我现在想打开上下文菜单时,我必须只单击 TextBlock。我怎样才能通过单击列表框项目的任何部分 - 列表框中的一行来打开上下文菜单?

为您的 ListBox 添加上下文菜单,这样无论您在 Listbox 上单击什么地方,都将获得上下文菜单

  <ListBox Name="Playlists_ListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2" >
                    <TextBlock Name="Name" Text="{Binding Title}"  Foreground="White"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ContextMenu>
            <ContextMenu StaysOpen="True">
                <MenuItem Header="Delete"/>
            </ContextMenu>
         </ListBox.ContextMenu>
  </ListBox>