开发者

How can I set the LISTBOX tooltip through XAML

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜