2

I've done some digging in trying to solve this, but haven't yet found a solution that works for me.

Basically I have an <ItemsControl> and in the <ItemsControl.ItemsTemplate> I have a <ListView> which displays a number of people's names. There will always be between 0 and 5 people's names listed.

What I want to do is have a tooltip popup with additional information relevant to the entity that is being hovered over. How do I get the index (or the content like the name) of the item I am currently hovering over though, to ensure that what the tooltip is displaying is for the correct person?!

I have a MouseEnter event on the listview which is triggered every time the mouse moves over an entity & in debug mode I can explore down into the sender details & can find the person's name that I want, but how do I get to it from code?

What I want is something like:

int index = sender.GetCurrentlyHoveredOverItem();

I don't want to overcomplicate this post by listing everything I've tried, but if you want any further info, please let me know.

Thanks in advance for your help!

H.B.
  • 166,899
  • 29
  • 327
  • 400
iambic77
  • 67
  • 7

3 Answers3

1

As others have hinted at, but not explicitly said, the items in the ListView should implement the tooltip directly, using an ItemTemplate if needed, rather than at a global level.

<ListViewItem ToolTipService.ToolTip="Tooltip for this item" />
Joel Lucsy
  • 8,520
  • 1
  • 29
  • 35
0

If you are using MVVM…

Bind IsMouseOver to a property in the VM, like "CurrentlyHoveredPersonList". Use OneWayToSource.

Create another VM property called "HoveredPersonListViewModel" that contains all the details you need for your tooltip. When CurrentlyHoveredPersonList gets set, populate HoveredPersonListViewModel and raise a property change notification. You'll get an actual reference to the object, so you might not need the index, but if you do you can use IndexOf(object) to get it from the source list right there in the view-model.

Bind the tooltip's DataContext to HoveredPersonListViewModel and its constituent controls to the appropriate properties thereof.

Jay
  • 56,361
  • 10
  • 99
  • 123
  • Unfortunately you can't bind `IsMouseOver` since it is read-only. It should be possible for sure to do a `OneWayToSource` binding but you get this error when trying it `error MC3065: 'IsMouseOver' property is read-only and cannot be set from markup` – Fredrik Hedblad Jan 27 '11 at 18:20
  • @Meleak, there are ways to push read-only properties into the ViewModel. See http://stackoverflow.com/q/1083224/87399. – Joe White Jan 27 '11 at 21:52
  • @Joe White: Yes, the problem can be worked around in many ways. That was not the meaning of my comment, thanks for pointing that out since I obviously didn't make that clear in my comment:) All I meant was that I think it should be possible to do a `OneWayToSource` binding for a read-only property (like `IsMouseOver`) – Fredrik Hedblad Jan 27 '11 at 22:04
  • Thanks for taking the time to look for a solution. I haven't tried this to see if it would work for me, instead I used an ItemTemplate (see H.B.'s post above). Maybe I was overcomplicating this & looking for a more difficult solution than needed, but it's useful to know what is possible :) – iambic77 Jan 28 '11 at 13:36
0

So you have a collection (ItemsControl) of lists (ListView) and you need the tooltip to be specific to a specific element within one of the internal lists, did i understand that correctly?

If so, why not create a ListView.ItemTemplate to take care of that?

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thanks H.B. I really need to learn more about templates because this is such a simple solution, but it did exactly what I wanted it to. I set a ListView.ItemTemplate with a TextBlock bound to the people's names and a MouseEnter event set on the TextBlock. The mouse event let me access the name of the currently hovered over item (or at least the currently moved over item) & I could then carry out the method to retrieve the relevant info for that person, which I then bound the TextBlock ToolTip to. Many thanks. – iambic77 Jan 28 '11 at 13:34