“Intercept” the opening of any tooltip appwide

跟風遠走 提交于 2019-12-07 03:11:02

问题


I want to show the text of a tooltip of any control in my wpf app inside a status bar, when a tooltip is about to be opened.

Of course I could try to loop recursively through all child controls of the main window and set their ToolTipOpening event to always the same method. But is there an easier way ?

Something like a Application.Current.AnyToolTipOpening event ?


回答1:


Sure, try this:

EventManager.RegisterClassHandler(typeof(FrameworkElement), FrameworkElement.ToolTipOpeningEvent, new ToolTipEventHandler(ToolTipHandler));

That registers a handler for all classes that derive from FrameworkElement.

Your handler method might look like this:

   private void ToolTipHandler(object sender, ToolTipEventArgs e) {
        // To stop the tooltip from appearing, mark the event as handled
        e.Handled = true; 
        FrameworkElement source = e.Source as FrameworkElement; 
        if (source != null) {
            MessageBox.Show(source.ToolTip.ToString()); // or whatever you like
        }
    }



回答2:


thanks , that worked. Additionally,to make the statusbar text disappear when the mouse leaves the control with the tooltip :

 EventManager.RegisterClassHandler(typeof(FrameworkElement),
                  MouseLeaveEvent, new MouseEventHandler(ClearText));


来源:https://stackoverflow.com/questions/1067899/intercept-the-opening-of-any-tooltip-appwide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!