简体   繁体   中英

how to right click on item from Listbox and open menu on WPF

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

          <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);
}

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:

<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);
}

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:

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.

3) Delete function works fine:

private void MenuItemDelete_Click(object sender, RoutedEventArgs e)
{
    if (someListBox.SelectedIndex == -1)
    {
        return;
    }

    someListBox.Items.RemoveAt(someListBox.SelectedIndex);
}

not need listBoxFiles_PreviewMouseRightButtonDown when you wrote

<ListBox>
            <ListBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Delete"></MenuItem>
                </ContextMenu>
            </ListBox.ContextMenu>
        </ListBox>

it is already working after right click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM