Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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);
}
like image 846
user979033 Avatar asked Mar 03 '12 19:03

user979033


2 Answers

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

Updated 5 March 2012:

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);
}
like image 155
Taras Feschuk Avatar answered Sep 21 '22 09:09

Taras Feschuk


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

like image 41
Stecenko Ruslan Avatar answered Sep 21 '22 09:09

Stecenko Ruslan