如何设置通过XAML列表框提示 [英] How can I set the LISTBOX tooltip through XAML

查看:111
本文介绍了如何设置通过XAML列表框提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中的列表框下的

 <列表框名称=lstName的DisplayMemberPath =LISTNAME工具提示={绑定路径= LISTNAME}/> 

我的要求是,什么都项目我在列表框中我展示,也应该出现在工具提示。即如果该项目是说:项目1,项目2等,然后当用户将通过鼠标点(悬停),以项目1时,toolltip应显示项目1。同为他人



所以,我的DisplayMemberPath设置到我应该显示属性(并正确推出)。然而,工具提示不来的。



该实体是在



 公共类ITEMLIST 
{
公共字符串LISTNAME {搞定;组; }
}



正在发生的事情是在


$结合b $ b

  this.lstName.ItemsSource =的GetData(); //假设数据来正确


解决方案

而不是设置在ListBox中的工具提示属性,将样式设置在ListBoxItems:

 <列表框名称=lstName的DisplayMemberPath =LISTNAME> 
< ListBox.ItemContainerStyle>
<风格的TargetType =ListBoxItem的>
< setter属性=工具提示VALUE ={结合LISTNAME}/>
< /样式和GT;
< /ListBox.ItemContainerStyle>
< /列表框>



这样的话,每个ListBoxItem的都会有自己的工具提示,显示该项目的价值。



既然你是直接设置的ItemsSource的列表框,你可能还没有设置一个DataContext,所以绑定不会在那里工作。如果你做的DataContext设置列表中,那么这将绑定,无论显示当前选择项作为提示,其中鼠标是在列表框。


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.

这篇关于如何设置通过XAML列表框提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆