Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. private void AddDoubleClickEventStyle(ListBox listBox, MouseButtonEventHandler mouseButtonEventHandler)
  2. {
  3. if (listBox.ItemContainerStyle == null)
  4. listBox.ItemContainerStyle = new Style(typeof(ListBoxItem));
  5. listBox.ItemContainerStyle.Setters.Add(new EventSetter()
  6. {
  7. Event = MouseDoubleClickEvent,
  8. Handler = mouseButtonEventHandler
  9. });
  10. }
  11.  
  12. //Usage:
  13. AddDoubleClickEventStyle(listView1, new MouseButtonEventHandler(listView1_MouseDoubleClick));
  14.  
  15. EventManager.RegisterClassHandler(typeof(ListBoxItem),
  16. ListBoxItem.MouseLeftButtonDownEvent,
  17. new RoutedEventHandler(this.MouseLeftButtonDownClassHandler));
  18.  
  19. private void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
  20. {
  21. }
  22.  
  23. <ListBox Name="testListBox">
  24. <ListBox.ItemContainerStyle>
  25. <Style TargetType="{x:Type ListBoxItem}">
  26. <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBox_MouseLeftButtonDown" />
  27. </Style>
  28. </ListBox.ItemContainerStyle>
  29. </ListBox>
  30.  
  31. myListBox.AddHandler(UIElement.MouseDownEvent,
  32. new MouseButtonEventHandler(ListBox_MouseDown), true);
  33.  
  34. <ListBox PreviewMouseDown="PlaceholdersListBox_OnPreviewMouseDown"/>
  35.  
  36. private void PlaceholdersListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
  37. {
  38. var item = ItemsControl.ContainerFromElement(sender as ListBox, e.OriginalSource as DependencyObject) as ListBoxItem;
  39. if (item != null)
  40. {
  41. // ListBox item clicked - do some cool things here
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement