Search code examples
c#wpfitemscontrolmouseclick-event

ItemsControl.Items.IndexOf(UIElem) returning different value/ Working out which item was clicked on


I have a list of ellipses (nodes) with various data attributed to each node. I add the ellipses in a ring onto the ItemsControl. The order in which I add them is the order I want to retain as I use this to find out which node I clicked on:

ItemsControl.Items.IndexOf(UIElem);

It works fine the first time, however the value returned for say, a node at a particular location changes.

I guess my question is does the ItemsControl retain the order in which I add the elements? Is there an easier way for me to work out which ellipse I clicked on? I have just been using the following:

Ellipse UIElem = (Ellipse)VisualTreeHelper.HitTest(ItemsControl, e.GetPosition(ItemsControl)).VisualHit)

All I want to do is have a tooltip show the name of the node.


Solution

  • Do you specify an event handler for each ellipse e.g. ButtonBase.Click? If yes then use the sender or e.OriginalSource properties of your handler function. You can also use the Tag property where you can Bind your item ellipse or whateverer else component you are trying to retrieve.

    Another more "wpf" solution would be to do it just in xaml with Ellipse.Triggers or Style.Triggers but in order to be more specific i would like to have a look at some xaml code - of your DataTemplate specifically.