Click here to Skip to main content
15,867,828 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
OK, I have looked everywhere for an answer to this but have found nothing. I have a StatusBar at the bottom of my window with a label inside it and a ListView on the right. The items will change throughout the execution of the program. Whenever the mouse is HOVERED over an item in the ListView I want to change the content of the label in the StatusBar to something. Like on a few programs when you hover over items in the menu you see a short description of what the action does appear in a StatusBar. I want to to this but with a ListView instead. I don't just want the text of the item to be mirrored in the label, I want to provide my own text for it. So I just need to get the currently hovered over item and then I can use if statements to display a message in the label. I have no idea how to do this (get the hovered item in the ListView). Thank you.
Posted

1 solution

An interesting question..


Lets say you have a list view like this:

HTML
<ListView>
    <ListView.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="ToolTip" Value="{Binding Content, RelativeSource={RelativeSource Mode=Self}}" />
        </Style>
    </ListView.Resources>
    <ListViewItem>1</ListViewItem>
    <ListViewItem>2</ListViewItem>
    <ListViewItem>3</ListViewItem>

</ListView>


You can retrieve it using MouseMoveEvent: (in MainWindow.xaml.cs)
C#
AddHandler(ListViewItem.MouseMoveEvent, (RoutedEventHandler)((o, e) =>
              {
                  if (e.Source is ListViewItem)
                  {
                      string tooltip = (e.Source as ListViewItem).ToolTip as string;
                      if (tooltip != null)
                          Title = tooltip;
                  }
                  Debug.WriteLine(e.Source);
              }));


If you want to hide the tooltip, you have to define another attached property, e.g.

C#
public static readonly DependencyProperty ToolTipProperty =
           DependencyProperty.RegisterAttached("ToolTip", typeof(string), typeof(MainWindow),
           new PropertyMetadata(""));

       public static void SetToolTip(DependencyObject obj, string value)
       {
           obj.SetValue(ToolTipProperty, value);
       }

       public static string GetToolTip(DependencyObject obj)
       {
           return (string)obj.GetValue(ToolTipProperty);
       }




Regards
Joseph Leung
 
Share this answer
 
v3
Comments
Henry Hunt 9-Nov-13 12:28pm    
But it doesn't work like that for a ListView item. Don't you have to determine the current pointer location and then get the item under it or something? That is the part I cannot do.
Leung Yat Chun 9-Nov-13 18:54pm    
In RoutedEventHandler, there are three properties can be used:
Sender (object that event handler attached to), OriginalSource (object that trigger up event) and Source (object that raise event) property.

You don't need to figure out the selected item by hit testing in this case, but if you want to do hit testing, you can try my UITools.cs
http://fileexplorer.codeplex.com/SourceControl/latest#src/FileExplorer3/app/FileExplorer3/UserControls/UITools.cs
Henry Hunt 11-Nov-13 12:55pm    
OK...I have found this code below:
private ListViewItem FindListViewItem(DragEventArgs e)
{
var visualHitTest = VisualTreeHelper.HitTest(dataTree, e.GetPosition(dataTree)).VisualHit;
ListViewItem listViewItem = null;
while (visualHitTest != null)
{
if (visualHitTest is ListViewItem)
{
listViewItem = visualHitTest as ListViewItem;
break;
}
else if (visualHitTest == dataTree)
{
MessageBox.Show("Found ListView instance");
return null;
}
visualHitTest = VisualTreeHelper.GetParent(visualHitTest);
}
return listViewItem;
}
...but I don't know how to use/call it because of the event in the parameters. Do I haver to attach it to an event in my ListView to use it or what? It was from here: http://stackoverflow.com/questions/12977046/finding-out-which-item-the-mouse-is-over-in-a-list-view-while-using-itemssource
Leung Yat Chun 11-Nov-13 13:22pm    
Hello, although you can obtain the ListViewItem via source property, you can use this method in UITools to retrieve the item using a Point:

///
/// Take a container (e.g. listbox) and a position and return the item (e.g. listboxItem) at that spot.
///

/// <param name="container"></param>
/// <param name="position"></param>
/// <returns>
public static I GetItemByPosition<i, c="">(C container, Point position)
where C : UIElement
where I : UIElement
{
DependencyObject obj = null;
//Bug#66
VisualTreeHelper.HitTest(
container,
(o) =>
{
if (UITools.FindAncestor(o) != null)
return HitTestFilterBehavior.Continue;
else return HitTestFilterBehavior.ContinueSkipSelf;
},
(r) =>
{
obj = r.VisualHit;
return HitTestResultBehavior.Stop;
},
new PointHitTestParameters(position));
//if (r == null) return null;

//DependencyObject obj = r.VisualHit;
while (!(obj is C) && (obj != null))
{
obj = VisualTreeHelper.GetParent(obj);

if (obj is I)
return obj as I;
}

return null;
}

And call it using this method:
lview.AddHandler(ListView.MouseMoveEvent, (MouseEventHandler)((o, e) =>
{
Debug .WriteLine(
UITools.GetItemByPosition<ListViewItem, ListView>(lview, e.GetPosition(lview)));
}));
Henry Hunt 11-Nov-13 13:43pm    
But that's the problem...I don't know how to use the method from that post: I mean you can't exactly go statusLabel.Content = new GlobalClasses().UpdateStatus(*** This is the bit I don't get. what do I do with this bit: DragEventArgs?***) or can you? That is what I want to know. I have never come across 'functions' with events in them apart from the obvious event handlers for controls but I wouldn't class that as a 'function' that I have written.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900