tags:

views:

29

answers:

2

I have a list box in WPF as under

<ListBox Name="lstName" DisplayMemberPath ="ListName" ToolTip="{Binding Path=ListName}" />

My requirement is that what ever items I am displaying in the listbox, should also appear in the tooltip. i.e. if the items are say "Item1", "Item2" etc. then when the user will point(hover) to "Item1" through mouse, the toolltip should display "Item1". Same for others

So my DisplayMemberPath is set to the Property which I am supposed to display (and it is coming properly). However, the tooltip is not coming at all.

The entity is as under

public class ItemList
{
  public string ListName { get; set; }
}

The binding is happening as under

this.lstName.ItemsSource = GetData(); // Assume that the data is coming properly
+2  A: 

Instead of setting the ToolTip property on the ListBox, set it on the ListBoxItems by applying a style:

<ListBox Name="lstName" DisplayMemberPath="ListName">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ToolTip" Value="{Binding ListName}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

That way, each ListBoxItem will have its own tooltip that displays the value for that item.

Since you are setting the ItemsSource on the ListBox directly, you probably haven't set a DataContext, so the Binding won't work there. If you do set the DataContext to the list, then that binding would display the currently selected item as the tooltip no matter where the mouse was on the ListBox.

Quartermeister
great .. that works.. thanks a lot
priyanka.sarkar_2
1 more question.. why your approach worked and not mine.
priyanka.sarkar_2
@priyanka: I updated my answer with a little more explanation. Did it answer your question? You were setting a tooltip on the ListBox, so it was binding relative to the DataContext of the ListBox, which I'm guessing was null. Setting it on the ListBoxItem will bind relative to the DataContext of the ListBoxItem, which will be the item from your ItemsSource list.
Quartermeister