How can I show a ToolTip for a short period of time after clicking on a Button using attached-behaviors?

289 Views Asked by At

I want to display a ToolTip of a Button immediately after clicking on it. The ToolTip should then disappear after a short period of time. This is needed only as a feedback for the user since clicking on the Button causes a string to be copied to the Clipboard.

I used an Button-Behavior to display the Tooltip of the Button via the Click-Event. To display the ToolTip I set its IsOpen property to true.

class ForceToolTipBehavior : Behavior<Button>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        this.AssociatedObject.Click += AssociatedObject_Click;
    }

    private void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        var tooltip = this.AssociatedObject.ToolTip as ToolTip;

        tooltip = new ToolTip();

        tooltip.Content = "Log was copied to your Clipboard";

        tooltip.IsOpen = true;        
    }
}

This works just fine but the ToolTip stays open. Is there any elegant way make the ToolTip disappear after like a second? Is it possible to use the ToolTipService for that task?

1

There are 1 best solutions below

0
AmRo On BEST ANSWER

Simple and quick way:

private void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
    var tooltip = this.AssociatedObject.ToolTip as ToolTip;

    tooltip = new ToolTip();

    tooltip.Content = "Log was copied to your Clipboard";

    tooltip.IsOpen = true;  

    HideToolTip(tooltip);   
}

private async void HideToolTip(ToolTip toolTip)
{
    await Task.Delay(3 * 1000); // 3 second
    toolTip.IsOpen = false;
}