C# 如何右键单击列表框中的项目并在 WPF 上打开菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9549231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to right click on item from Listbox and open menu on WPF
提问by user979033
i have Listbox with files in, i want to able to right click and open a menu like Delete in order to remove files from the Listbox.
我有包含文件的列表框,我希望能够右键单击并打开像“删除”这样的菜单,以便从列表框中删除文件。
currently i have this function after right click on item inside my Listbox
目前我在我的列表框中右键单击项目后有这个功能
private void listBoxFiles_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
}
and i implement at XAML Delete menu after right click
我在右键单击后在 XAML 删除菜单中实现
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete"/>
</ContextMenu>
</ListBox.ContextMenu>
the function who delete file from my ListBox:
从我的列表框中删除文件的函数:
private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
if (listBoxFiles.SelectedIndex == -1)
{
return;
}
//string filePath = (listBoxFiles.SelectedItem).ToString();
int index = listBoxFiles.SelectedIndex;
listBoxFiles.Items.RemoveAt(index);
}
回答by Stecenko Ruslan
not need listBoxFiles_PreviewMouseRightButtonDownwhen you wrote
listBoxFiles_PreviewMouseRightButtonDown你写的时候不需要
<ListBox>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete"></MenuItem>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
it is already working after right click
右键单击后它已经在工作
回答by Taras Feschuk
You already have a context menu with your markup.
您已经有一个带有标记的上下文菜单。
If you want to perform some operation, one of the ways is to check which item was clicked in the menu's Click function. For example, you have the next listbox:
如果要执行某些操作,其中一种方法是检查菜单的 Click 功能中单击了哪个项目。例如,您有下一个列表框:
<ListBox Name="someListBox">
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
</ContextMenu>
</ListBox.ContextMenu>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
</ListBox>
And function may be next:
功能可能是下一个:
private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
if (someListBox.SelectedIndex == -1) return;
// Hypothetical function GetElement retrieves some element
var element = GetElement(someListBox.SelectedIndex);
// Hypothetical function DeleteElement
DeleteElement(element);
}
Updated 5 March 2012:
2012 年 3 月 5 日更新:
Here is another variant of your listbox. You can add a context menu not to listbox but to the listbox items. For example:
这是列表框的另一个变体。您可以不向列表框添加上下文菜单,而是向列表框项添加上下文菜单。例如:
<ListBox Name="someListBox" MouseDown="someListBox_MouseDown">
<ListBox.Resources>
<!--Defines a context menu-->
<ContextMenu x:Key="MyElementMenu">
<MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
</ContextMenu>
<!--Sets a context menu for each ListBoxItem in the current ListBox-->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContextMenu" Value="{StaticResource MyElementMenu}"/>
</Style>
</ListBox.Resources>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
</ListBox>
1) This function will unsellect all items when you clicked on the empty space in the listbox:
1) 当您单击列表框中的空白区域时,此功能将取消选择所有项目:
private void someListBox_MouseDown(object sender, MouseButtonEventArgs e)
{
someListBox.UnselectAll();
}
2) When you click the lisboxt item, it is blue. When you right click the listbox item, it is still blue, but if a context menu appears, the listbox item becomes gray, maybe it is so because this item loses a focus.
2) 当您单击 lisboxt 项时,它是蓝色的。当您右键单击列表框项时,它仍然是蓝色的,但是如果出现上下文菜单,则列表框项变为灰色,可能是因为该项失去了焦点。
3) Delete function works fine:
3)删除功能工作正常:
private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
if (someListBox.SelectedIndex == -1)
{
return;
}
someListBox.Items.RemoveAt(someListBox.SelectedIndex);
}

