private async void ContextMenu1_Click(object sender, RoutedEventArgs e) { var item = sender as MenuItem; if (item == null) { return; } var menu = item.Parent as ContextMenu; switch (item.Header.ToString()) { case "Copy": FileManipulator.CopyFile(selectedListItem); break; case "Paste": FileManipulator.PasteFileBy(ListView1.IsFocused ? _graphics1.Path : _graphics2.Path); break; case "Cut": FileManipulator.CutFile(selectedListItem); break; case "Delete": FileManipulator.DeleteFile(selectedListItem); break; case "Archive": ShowChooseDialog(selectedListItem, myAction.Archive); break; case "Unarchive": UnziperArchives.UnarchiveElemInThread(selectedListItem); break; case "Rename": // Getting the currently selected ListBoxItem // Note that the ListBox must have // IsSynchronizedWithCurrentItem set to True for this to work ListViewCustom myListBox = menu.Name == "ContextMenu1" ? ListView1 : ListView2; ListViewItem myListBoxItem = (ListViewItem)(myListBox.ItemContainerGenerator.ContainerFromItem(selectedListItem)); // Getting the ContentPresenter of myListBoxItem ContentPresenter myContentPresenter = FindVisualChild <ContentPresenter>(myListBoxItem); // Finding textBlock from the DataTemplate that is set on that ContentPresenter DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; _myTextBox = (TextBox)myDataTemplate.FindName("ItemNameBox", myContentPresenter); // Do something to the DataTemplate-generated TextBlock _myTextBox.IsReadOnly = false; _myTextBox.SelectionStart = 0; _myTextBox.SelectionLength = _myTextBox.Text.Length; KeyDown += Rename_KeyDown; break; case "Statistic": MessageBox.Show(await LogicForUi.ReadStatisticAsync(selectedListItem)); break; case "Decode": selectedListItem.AcceptDecode(new AesVisitor()); break; case "Encode": selectedListItem.AcceptEncode(new AesVisitor()); break; } }