code_junkie

How can I set different Tooltip text for each item in a listbox?

Nov 11th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. private ITypeOfObjectsBoundToListBox DetermineHoveredItem()
  2. {
  3. Point screenPosition = ListBox.MousePosition;
  4. Point listBoxClientAreaPosition = listBox.PointToClient(screenPosition);
  5.  
  6. int hoveredIndex = listBox.IndexFromPoint(listBoxClientAreaPosition);
  7. if (hoveredIndex != -1)
  8. return listBox.Items[hoveredIndex] as ITypeOfObjectsBoundToListBox;
  9. else
  10. return null;
  11. }
  12.  
  13. public static class MouseInput
  14. {
  15. // TME_HOVER
  16. // The caller wants hover notification. Notification is delivered as a
  17. // WM_MOUSEHOVER message. If the caller requests hover tracking while
  18. // hover tracking is already active, the hover timer will be reset.
  19.  
  20. private const int TME_HOVER = 0x1;
  21.  
  22. private struct TRACKMOUSEEVENT
  23. {
  24. // Size of the structure - calculated in the constructor
  25. public int cbSize;
  26.  
  27. // value that we'll set to specify we want to start over Mouse Hover and get
  28. // notification when the hover has happened
  29. public int dwFlags;
  30.  
  31. // Handle to what's interested in the event
  32. public IntPtr hwndTrack;
  33.  
  34. // How long it takes for a hover to occur
  35. public int dwHoverTime;
  36.  
  37. // Setting things up specifically for a simple reset
  38. public TRACKMOUSEEVENT(IntPtr hWnd)
  39. {
  40. this.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT));
  41. this.hwndTrack = hWnd;
  42. this.dwHoverTime = SystemInformation.MouseHoverTime;
  43. this.dwFlags = TME_HOVER;
  44. }
  45.  
  46. }
  47.  
  48. // Declaration of the Win32API function
  49. [DllImport("user32")]
  50. private static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);
  51.  
  52. public static void ResetMouseHover(IntPtr windowTrackingMouseHandle)
  53. {
  54. // Set up the parameter collection for the API call so that the appropriate
  55. // control fires the event
  56. TRACKMOUSEEVENT parameterBag = new TRACKMOUSEEVENT(windowTrackingMouseHandle);
  57.  
  58. // The actual API call
  59. TrackMouseEvent(ref parameterBag);
  60. }
  61.  
  62. <ListBox Width="400" Margin="10" ItemsSource="{Binding Source={StaticResource myTodoList}}">
  63. <ListBox.ItemTemplate>
  64. <DataTemplate>
  65. <TextBlock Text="{Binding Path=TaskName}" ToolTipService.ToolTip="{Binding Path=TaskName}"/>
  66. </DataTemplate>
  67. </ListBox.ItemTemplate>
  68. </ListBox>
  69.  
  70. int hoveredIndex = -1;
  71.  
  72. private void lstCars_MouseMove(object sender, MouseEventArgs e)
  73. {
  74. int newHoveredIndex = lstCars.IndexFromPoint(e.Location);
  75.  
  76. if (hoveredIndex != newHoveredIndex)
  77. {
  78. hoveredIndex = newHoveredIndex;
  79. if (hoveredIndex > -1)
  80. {
  81. tt1.Active = false;
  82. tt1.SetToolTip(lstCars, ((Car)lstCars.Items[hoveredIndex]).InsuranceGroup);
  83. tt1.Active = true;
  84. }
  85. }
  86. }
  87.  
  88. <Style x:Key="radioListBox" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
  89. <Setter Property="BorderThickness" Value="0" />
  90. <Setter Property="Margin" Value="5" />
  91. <Setter Property="Background" Value="{x:Null}" />
  92. <Setter Property="ItemContainerStyle">
  93. <Setter.Value>
  94. <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
  95. <Setter Property="Template">
  96. <Setter.Value>
  97. <ControlTemplate TargetType="ListBoxItem">
  98. <Grid Background="Transparent">
  99. <RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}" Content="{Binding MyName}"/>
  100. </Grid>
  101. </ControlTemplate>
  102. </Setter.Value>
  103. </Setter>
  104. <Setter Property="ToolTip" Value="{Binding MyToolTip}" />
  105. </Style>
  106. </Setter.Value>
  107. </Setter>
  108. </Style>
Add Comment
Please, Sign In to add comment