How to right click on item from Listbox and open menu on WPF?

Not need listBoxFiles_PreviewMouseRightButtonDown when you wrote.

Up vote 0 down vote favorite share g+ share fb share tw.

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 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); } c# wpf link|improve this question edited Mar 5 at 12:44 asked Mar 3 at 19:41user979033486 50% accept rate.

Not need listBoxFiles_PreviewMouseRightButtonDown when you wrote it is already working after right click.

Thanks, I will try this at the evening and update. – user979033 Mar 4 at 10:27 OK thanks, now how can I "catch" the file name (path) I clicked on? – user979033 Mar 4 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 at 21:42 Error: Unable to cast object of type 'System. String' to type 'System.Windows.Controls. ListBoxItem'.

– user979033 Mar 5 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 at 12:56.

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

Thanks, I will try this at the evening and update. – user979033 Mar 4 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 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 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 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 at 10:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions