开发者

How to make a tooltip appear immediately in Silverlight?

开发者 https://www.devze.com 2022-12-24 01:06 出处:网络
In WPF, I get a tooltip to appear immediately开发者_如何学Python like this: TextBlock tb = new TextBlock();

In WPF, I get a tooltip to appear immediately开发者_如何学Python like this:

TextBlock tb = new TextBlock();
tb.Text = name;
ToolTip tt = new ToolTip();
tt.Content = "This is some info on " + name + ".";
tb.ToolTip = tt;
tt.Cursor = Cursors.Help;
ToolTipService.SetInitialShowDelay(tb, 0);

This makes the user experience better since if the user wants to look at the tooltips of five items on the page, he doesn't have to wait that long second for each one.

But since Silverlight does not have SetInitialShowDelay, what is a workaround to make the tooltip appear immediately?


You'll need to hook the MouseEnter event and show it straight away yourself:-

    TextBlock tb = new TextBlock(); 
    tb.Text = name; 
    ToolTip tt = new ToolTip(); 
    tt.Content = "This is some info on " + name + "."; 
    ToolTipService.SetToolTip(tb, tt);
    tb.MouseEnter += (s, args) => {  
      ((ToolTip)ToolTipService.GetToolTip((DependencyObject)s)).IsOpen = true;
    };


Other than re-implementing the mouse enter (or the whole tooltip service), I'm afraid you might be out of luck - the delay you see is actually hard-coded into the "OnOwnerMouseEnter" method of the TooltipService:

(courtesy of Reflector)

    TimeSpan span = (TimeSpan) (DateTime.Now - _lastToolTipOpenedTime);
    if (TimeSpan.Compare(span, new TimeSpan(0, 0, 0, 0, 100)) <= 0)
    {
        OpenAutomaticToolTip(null, EventArgs.Empty);
    }
    else
    {
        if (_openTimer == null)
        {
            _openTimer = new DispatcherTimer();
            _openTimer.Tick += new EventHandler(ToolTipService.OpenAutomaticToolTip);
        }
        _openTimer.Interval = new TimeSpan(0, 0, 0, 0, 400);
        _openTimer.Start();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号