locked
Setting the ToolTip's duration RRS feed

  • Question

  • Well, I hope I can explain this without including any code:

    I have a canvas to which I sign up for the MouseMove event.  Every time the mouse moves over my canvas, my MouseMove function is called and inside this function I create a tooltip object tt if tt is null.  I then set the content of the tt as well as its IsOpen property.  Essentailly I have something like this:

    private void MouseMove(object sender, MouseEvent e){

    if(tt == null) tt = new ToolTip();

    tt.Content("count: "+ this.count);

    tt.IsOpen = true;

    this.count++;

    }

    How can I set the show duration of my ToolTip tt?  I've tried doing the following:

    ToolTipService.SetShowDuration(tt, 3000);

    but to no avail?


    MarcinMR

    Thursday, October 15, 2015 9:19 PM

Answers

  • >>How can I set the show duration of my ToolTip tt?

    If you want the ToolTip to disappear 3 seconds after the the mouse has stopped moving over your Canvas you could use a Timer. Please refer to the following example:

    public partial class MainWindow : Window
        {
            ToolTip tt;
            int count;
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            public MainWindow()
            {
                InitializeComponent();
                timer.Interval = TimeSpan.FromSeconds(3);
                timer.Tick += timer_Tick;
            }
    
            private void Canvas_MouseMove_1(object sender, MouseEventArgs e) {
              if (tt == null)
                tt = new ToolTip();
    
              tt.Content = "count: " + this.count;
              tt.IsOpen = true;
              //reset timer
              timer.Stop();
              timer.Start();
    
              this.count++;
            }
    
            void timer_Tick(object sender, EventArgs e) {
              if(tt != null)
                tt.IsOpen = false;
              timer.Stop();
            }
        }
    
     <Canvas Background="Yellow" Width="100" Height="100" MouseMove="Canvas_MouseMove_1"></Canvas>

    Hope that helps.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by MarcinMR Friday, October 16, 2015 5:43 PM
    Friday, October 16, 2015 4:46 PM

All replies

  • A tooltip is associated with a control.  A tooltip with no control most likely won't work like you want it to.

    An alternative is a Popup.


    Lloyd Sheen

    Thursday, October 15, 2015 10:27 PM
  • I put together an experiment:

        <Grid Name="LayoutRoot">
            <Canvas MouseMove="Canvas_MouseMove" Width="200" Height="200" Background="Pink">
                <Canvas.ToolTip>
                    <ToolTip x:Name="tt">
                       <TextBlock Name="tb"/>
                    </ToolTip>
                </Canvas.ToolTip>
            </Canvas>
        </Grid>

    and

            

    int count=0;

    private void Canvas_MouseMove(object sender, MouseEventArgs e) { tb.Text = count.ToString(); ToolTipService.SetShowDuration(tt, 3000); tt.IsOpen = true; count++; }


    If you're not too fussed about where the tooltip appears then that works ok.

    If you want it to follow the mouse round you could do:

            private void Canvas_MouseMove(object sender, MouseEventArgs e)
            {
                tb.Text = count.ToString();
                //    ToolTipService.SetShowDuration(tt, 1);
                tt.IsOpen = false;
                tt.IsOpen = true;
                count++;
            }

    The tooltip then disappears when you move the mouse.

    As Lloyd suggested you could alternatively go with a popup and position it relative to the cursor.

    Depends what you want and why you're creating ui in code.


    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Friday, October 16, 2015 8:10 AM
  • >>How can I set the show duration of my ToolTip tt?

    If you want the ToolTip to disappear 3 seconds after the the mouse has stopped moving over your Canvas you could use a Timer. Please refer to the following example:

    public partial class MainWindow : Window
        {
            ToolTip tt;
            int count;
            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
            public MainWindow()
            {
                InitializeComponent();
                timer.Interval = TimeSpan.FromSeconds(3);
                timer.Tick += timer_Tick;
            }
    
            private void Canvas_MouseMove_1(object sender, MouseEventArgs e) {
              if (tt == null)
                tt = new ToolTip();
    
              tt.Content = "count: " + this.count;
              tt.IsOpen = true;
              //reset timer
              timer.Stop();
              timer.Start();
    
              this.count++;
            }
    
            void timer_Tick(object sender, EventArgs e) {
              if(tt != null)
                tt.IsOpen = false;
              timer.Stop();
            }
        }
    
     <Canvas Background="Yellow" Width="100" Height="100" MouseMove="Canvas_MouseMove_1"></Canvas>

    Hope that helps.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by MarcinMR Friday, October 16, 2015 5:43 PM
    Friday, October 16, 2015 4:46 PM
  • That worked....

    I also tried O'Neil's answer, however, half the time it didn't work.  That is to say, half the time it wouldn't disappear.  Curious why?  Thankfully in this field there often is more than one way to skin a cat.

    Thanks to both of you.


    MarcinMR

    Friday, October 16, 2015 5:45 PM