WPF 工具提示不更新 [英] WPF ToolTip does not update

查看:19
本文介绍了WPF 工具提示不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个代表工作人员的简单类

Assuming I have a simple class that represents a staff member

class Staff
{
    public string FirstName { get; set; }
    public string FamilyName { get; set; }
    public int SecondsAlive { get; set; }        
}

我有一个供员工使用的数据模板

and I have a DataTemplate for staff

<DataTemplate DataType={x:Type Staff}>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text={Binding FirstName}/>
        <TextBlock Text=" ">
        <TextBlock Text={Binding FamilyName}/>
        <StackPanel.ToolTip>
            <TextBlock Text={Binding SecondsAlive}/>
        </StackPanel.ToolTip>
     </StackPanel>
</DataTemplate>

然后我在 ListBox 中展示了一大群员工

I then show a whole bunch of staff in a ListBox

myListBox.ItemsSource = GetAllStaff();

非常标准的东西.我遇到的问题是,显示某人活着的秒数的工具提示没有更新.当您第一次将鼠标悬停在工作人员上时,它可以正常工作,但从那时起,它就会永远保持该值.我可以实施 INotifyPropertyChanged 来解决这个问题,但是每当 SecondsAlive 发生变化时,为每个工作人员执行此操作似乎有点过分.假设列表中有 400 名员工,那么即使用户可能永远不会查看其他工具提示,我也必须引发 400 个事件.我想要的是让工具提示在每次显示时都请求 SecondsAlive 属性.这可能吗?

Pretty standard stuff. The problem I have is that the tooltip which shows the number of seconds that someone has been alive does not get updated. When you first mouse over a staff member then it works fine but from then on it keeps that value for ever. I could implement INotifyPropertyChanged to get around this but it seems like overkill to do this for every staff member whenever SecondsAlive changes. Say I have 400 staff in the list then I have to raise 400 events even though the user might never look at another tooltip. What I would like is to make the tooltip request the SecondsAlive property ever time it is shown. Is that possible?

请注意,这只是一个例子,我不需要知道我的员工还活着多少秒 :-) 但我有同样的问题,我需要为工具提示甚至提高大约 400 次有人可能不会看.

Please note that this is just an example and I don't need to know how many seconds my staff have been alive :-) But I have the same issue that I need to raise an even around 400 times just for a tooltip which someone probably won't look at.

推荐答案

OMG!!!我终于找到了这个问题的解决方案!!!这几个月来一直困扰着我.我并不惊讶没有人回答这个问题,因为我在顶部输入的代码实际上没有显示我试图重现的问题,实际上它显示了解决方案.答案是,如果你像这样定义工具提示

OMG!!! I have finally found the solution to this problem!!! This has been bugging me for months. I'm not surprised no one answered this because the code I typed out at the top actually DIDN'T show the problem I was trying to reproduce, in fact it showed the solution. The answer is that if you define your tooltip like this

    <StackPanel.ToolTip>
        <TextBlock Text="{Binding SecondsAlive}"/>
    </StackPanel.ToolTip>

然后一切正常,而且没有必要在SecondsAlive"上引发 propertyChanged 事件.每次显示工具提示时,框架都会调用 SecondsAlive 属性.当您像这样定义工具提示时,问题就出现了:

Then everything works just fine and dandy and there is no need to raise a propertyChanged event on "SecondsAlive". The framework will call the SecondsAlive property every time the tooltip is shown. The problem comes when you define your tooltip like this:

    <StackPanel.ToolTip>
        <ToolTip>
            <TextBlock Text="{Binding SecondsAlive}"/>
        </ToolTip>
    </StackPanel.ToolTip>

在那里有额外的工具提示标签是有道理的,当然你需要创建一个工具提示对象来将它分配给工具提示属性,但这是不正确的.您分配给工具提示属性的实际上是工具提示的内容.我假设您需要为其提供控件,例如要显示的文本块和图像,但您可以传入任何内容,它会像内容控件一样显示内容.看到它继承自内容控制,这是有道理的:-) 一旦你知道,这一切似乎很明显:-)

Having the extra tooltip tag in there makes sense, surely you need to create a tooltip object to assign it to the tooltip property but this is incorrect. What you are assigning to the tooltip property is actually the content of the tooltip. I was assuming you needed to give it controls such as textblock and image to display but you can pass in anything and it will display the content just like a content control. Seeing it inherits from content control this makes sense :-) It all seems obvious once you know :-)

感谢大家看到这里.

附注.我发现了一个额外的问题,即简化代码的下一个合乎逻辑的步骤是像这样直接将文本分配给工具提示(假设您的工具提示是纯文本):

PS. I found an additional problem in that the next logical step in simplifying code is to just assign text straight to the tooltip like this (assuming your tooltip is plain text):

 <TextBlock Text="{Binding Path=StaffName}" ToolTip="{Binding Path=StaffToolTip}"/>

这也导致了我遇到的原始问题.这是有道理的,因为属性 StaffToolTip 的结果被分配给工具提示属性并且永远不会被再次调用.然而,为什么将一个 TextBlock 分配给 tooltip 属性实际上解决了这个问题,这并没有多大意义.

This also causes the original problem I was having. This makes sense because the results of the property StaffToolTip get assigned to the tooltip property and never get called again. However, it doesn't quite make sense why then assigning a TextBlock to the tooltip property actually solves the problem.

这篇关于WPF 工具提示不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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