Right-click the item in ListBox and right-click the item in listbox.
Today, I want to create a ListBox item and right-click the pop-up menu. There is a "RENAME" on the menu to rename the items in the ListBox. A problem is found during implementation, right-click the blank area of ListBox and rename it. Because no selected item exists, the last selected item will be renamed. Check it online and find a blog to solve similar problems, the solution is to capture the PreviewMouseButtonDown event of ListBox, and then judge whether the mouse clicks the item in ListBox Based on Sender and EventArgs, because my project uses the MVVM mode, therefore, it is difficult to pass Sender and EventArgs to ViewModel. Finally, I can use the MvvmLight library to upload a RouteEventArgs on the Internet. Then I tried to get it. The Code is as follows:
1 xmlns:cmd="http://www.galasoft.ch/mvvmlight"2 3 4 <i:EventTrigger EventName="PreviewMouseRightButtonDown" >5 <cmd:EventToCommand Command="{Binding cmdListBoxPreMouseRBtnDown}" PassEventArgsToCommand="True"/>6 </i:EventTrigger>
XAML code
1 bool isItemSelected = false; // Determines whether any items are selected when the rename is already Med 2 private void reset (RoutedEventArgs e) 3 {4 MouseButtonEventArgs mouseEventArgs = (MouseButtonEventArgs) e; 5 object item = GetElementFromPoint (ItemsControl) e. source, mouseEventArgs. getPosition (ItemsControl) e. source); 6 // use e. source to obtain the control where the event is sent, which is equivalent to the object sender. Therefore, this RouteEventArgs has two parameters (I think so now) of 7 // PreviewMouseRightButtonDown (object sender, MouseEventArgs e) 8 // The GetElementFromPoint method returns the clicked item based on the control clicked by the mouse and the point clicked by the mouse relative to the position of the control. If the item is clicked, the item is returned, if it is not clicked, null 9 isItemSelected = (item! = Null); 10} 11 12 private object GetElementFromPoint (ItemsControl itemsControl, System. windows. point point) 13 {14 UIElement element = itemsControl. inputHitTest (point) as UIElement; 15 // InputHitTest of ItemsControl can obtain the elements at the innermost layer of the clicked link, rather than the innermost layer of the inheritance relationship, 16 // It is the innermost layer of the UI combination method, because if I click a blank space, the order of getting the parent element is ScrollViewer-> Border-> ItemsControl17 // I checked MSDN, these elements do not inherit from the 18 while (element! = Null) // returns null19 {20 if (element = itemsControl) if nothing is reached. // if ItemsControl is used, it is clicked on ListBox, if the value is not reached, the system returns null21 return null; 22 object item = itemsControl. itemContainerGenerator. itemFromContainer (element); 23 // the function of the above Code is to obtain the item that can be displayed on the UI (I guess). For example, if the element is ListBoxItem, then the text of 24 // ListBoxItem will be returned ("TEMPLATE 1", "template 2 "...) 25 if (! Item. equals (DependencyProperty. unsetValue) // If NO content is obtained above, the item value is DependencyProperty. unsetValue26 return item; 27 // then obtain the parent level of the element until ItemsControl. itemContainerGenerator. itemFromContainer (element) gets content 28 // or the element is already ItemsControl29 element = (UIElement) VisualTreeHelper. getParent (element); 30} 31 return null; 32}
ViewModel code
Well, here it is done, this is my reference to the great god post http://www.cnblogs.com/TianFang/archive/2010/02/10/1667186.html, he also has a way to capture both ListBox and ListBoxItem PreviewMouseRightButtonDown event, then, set IsItemSeleted to false in The ListBox event, and set it to true in the ListBoxItem event. This is fine, but my ListBox items are automatically generated, I don't know where to set it, so I used the first one. If you know it, please let me know. Thank you. In the future, we will record the problems solved every day to deepen our understanding and memory. This is a step closer to becoming a great god's goal, Fighting!