如何设置不同的工具提示文本在ListBox每一个项目? [英] How can I set different Tooltip text for each item in a listbox?

查看:193
本文介绍了如何设置不同的工具提示文本在ListBox每一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据绑定到对象的集合列表框。列表框被配置为显示各对象的标识符属性。我想显示具体到正在上空盘旋,而不是一个提示中的列表框作为一个整体的列表框内部的项目信息的工具提示。

I have a listbox that is databound to a Collection of objects. The listbox is configured to display an identifier property of each object. I would like to show a tooltip with information specific to the item within the listbox that is being hovered over rather than one tooltip for the listbox as a whole.

我的WinForms和感谢内工作放在一起pretty的很好的解决方案了一些有用的博客文章,我想和大家分享。

I am working within WinForms and thanks to some helpful blog posts put together a pretty nice solution, which I wanted to share.

我会很感兴趣地看,如果有任何其他优雅的解决这个问题,不然怎么可能在WPF来完成。

I'd be interested in seeing if there's any other elegant solutions to this problem, or how this may be done in WPF.

推荐答案

有两个主要的子问题必须解决,才能解决这个问题:

There are two main sub-problems one must solve in order to solve this problem:

  1. 确定哪些项目正在上空盘旋
  2. 获取MouseHover事件火灾,当用户一直徘徊在一个项目,然后移动光标列表框之内,并悬停另一个项目。

的第一个问题是相当简单的解决。通过调用喜欢你的处理程序MouseHover在下面的方法,你可以决定哪些项目正在上空盘旋:

The first problem is rather simple to solve. By calling a method like the following within your handler for MouseHover, you can determine which item is being hovered over:

    private ITypeOfObjectsBoundToListBox DetermineHoveredItem()
    {
        Point screenPosition = ListBox.MousePosition;
        Point listBoxClientAreaPosition = listBox.PointToClient(screenPosition);

        int hoveredIndex = listBox.IndexFromPoint(listBoxClientAreaPosition);
        if (hoveredIndex != -1)
            return listBox.Items[hoveredIndex] as ITypeOfObjectsBoundToListBox;
        else
            return null;        
    }

然后使用返回值来设置工具提示需要。

Then use the returned value to set the tool-tip as needed.

第二个问题是,通常MouseHover事件不会再次发射,直到光标离开控件的客户区,然后再回来。

The second problem is that normally the MouseHover event isn't fired again until the cursor has left the client area of the control and then come back.

您可以通过包装TrackMouseEvent的Win32 API调用解决这个问题。在下面的code时,ResetMouseHover方法包装API调用来获得预期的效果:复位控制时,悬停事件被激发潜在的计时器

You can get around this by wrapping the TrackMouseEvent Win32API call. In the following code, the ResetMouseHover method wraps the API call to get the desired effect: reset the underlying timer that controls when the hover event is fired.

public static class MouseInput
{
    // TME_HOVER
    // The caller wants hover notification. Notification is delivered as a 
    // WM_MOUSEHOVER message.  If the caller requests hover tracking while 
    // hover tracking is already active, the hover timer will be reset.

    private const int TME_HOVER = 0x1;

    private struct TRACKMOUSEEVENT
    {
        // Size of the structure - calculated in the constructor
        public int cbSize;

        // value that we'll set to specify we want to start over Mouse Hover and get
        // notification when the hover has happened
        public int dwFlags;

        // Handle to what's interested in the event
        public IntPtr hwndTrack;

        // How long it takes for a hover to occur
        public int dwHoverTime;

        // Setting things up specifically for a simple reset
        public TRACKMOUSEEVENT(IntPtr hWnd)
        {
            this.cbSize = Marshal.SizeOf(typeof(TRACKMOUSEEVENT));
            this.hwndTrack = hWnd;
            this.dwHoverTime = SystemInformation.MouseHoverTime;
            this.dwFlags = TME_HOVER;
        }

    }

    // Declaration of the Win32API function
    [DllImport("user32")]
    private static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);

    public static void ResetMouseHover(IntPtr windowTrackingMouseHandle)
    {
        // Set up the parameter collection for the API call so that the appropriate
        // control fires the event
        TRACKMOUSEEVENT parameterBag = new TRACKMOUSEEVENT(windowTrackingMouseHandle);

        // The actual API call
        TrackMouseEvent(ref parameterBag);
    }

}

有了包装,你可以简单地调用ResetMouseHover(listBox.Handle)在您MouseHover处理程序的结束和悬停事件会再次触发,即使当光标停留在控制的范围之内。

With the wrapper in place, you can simply call ResetMouseHover(listBox.Handle) at the end of your MouseHover handler and the hover event will fire again even when the cursor stays within the control's bounds.

我敢肯定,这种做法,坚持所有的code在MouseHover处理程序必须导致更多MouseHover射击事件比是真的有必要,但它会完成这项工作。任何改进非常欢迎。

I'm sure this approach, sticking all the code in the MouseHover handler must result in more MouseHover events firing than are really necessary, but it'll get the job done. Any improvements are more than welcome.

这篇关于如何设置不同的工具提示文本在ListBox每一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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