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

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

问题描述

假设我有一个重新presents一个简单的类工作人员

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; }        
}

和我有一个DataTemplate员工

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>

我再展一大堆工作人员在一个列表框

I then show a whole bunch of staff in a listbox

myListBox.ItemsSource = GetAllStaff();

pretty的标准的东西。我的问题是,它显示秒,有人一直活仔数工具提示没有更新。当你第一次鼠标移到一位工作人员则它工作正常,但从那时起,它保持该值,直到永远。我可以实现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!我终于找到了解决这个问题!这已被窃听了我好几个月。我并不感到惊讶没有人回答这是因为code我输入了在上面居然没有显示问题,我试图重现,事实上它显示的解决方案。答案是,如果你定义你的提示是这样

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>

然后,一切都工作得非常愉快,也没有必要提出一个PropertyChanged事件的SecondsAlive。该框架将显示工具提示每次调用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。我发现了另外一个问题在于,在简化code中的下一个逻辑步骤是只分配连胜文像这样的提示(假设你的提示是纯文本):

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的工具提示属性实际上解决了这个问题。

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天全站免登陆