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

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

shareimprove this answer

edited Mar 5 '12 at 20:12

answered Mar 3 '12 at 20:06

Taras Feschuk

599158

  • thanks, i will try this at the evening and update. – user979033 Mar 4 '12 at 10:27

  • i want to add 3 functions in the menu so it's still necessary to check which item was clicked ? and i did not understand the last 2 lines in the function s MenuItemDelete_Click – user979033 Mar 4 '12 at 21:32

  • i want to add 3 functions in the menu (each option in the menu will have different function) so it's still necessary to check which item was clicked ? and i did not understand the last 2 lines in the function s MenuItemDelete_Click – user979033 Mar 4 '12 at 21:49

  • Because all list elements share a context menu, you should check which item was clicked. The last 2 lines are not necessary, i wrote them just to show some example. You should determine which element was clicked and perform some operations with that element. – Taras Feschuk Mar 5 '12 at 10:22 

  • If you want to retrieve a text value of clicked element you can write: string elementText =((ListBoxItem)someListBox.SelectedItem).Content.ToString(); – Taras Feschuk Mar 5 '12 at 10:32

show 5 more comments

1

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

shareimprove this answer

answered Mar 3 '12 at 19:48

Stecenko Ruslan

26313

  • thanks, i will try this at the evening and update. – user979033 Mar 4 '12 at 10:27

  • OK thanks, now how can i "catch" the file name (path) i clicked on ? – user979033 Mar 4 '12 at 21:11 

  • i want to add 3 functions in the menu (each option in the menu will have different function) so it's still necessary to check which item was clicked ? and i did not understand the last 2 lines in the function s MenuItemDelete_Click – user979033 Mar 4 '12 at 21:42 

  • Error: Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ListBoxItem'. – user979033 Mar 5 '12 at 10:55

  • it's OK now but i have another questions: 1. why each pressing on item in the Listbox change it's status from selected (color blue) to not selected and if file is selected and i press on an empty space on the Listbox the file remained selected ? 2. i implement function that if i press right click on item in the Listbox and in the menu press Delete the item removed from my List box but this happen only if the file not selected before, i mean if i press on item in the Listbox and than right click and remove the file remined in the Listbox and not deleted, please see my update – user979033 Mar 5 '12 at 12:56

 转载

猜你喜欢

转载自blog.csdn.net/qq_28368039/article/details/85759266