Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ToolTip disappears on mouse down

Tags:

wpf

tooltip

mouse

I've got tooltip on an element that I want to stay open even when the user clicks or holds the mouse button down while over my element.

Is there anyway to do this?

like image 529
viggity Avatar asked Mar 11 '10 15:03

viggity


2 Answers

There is a StaysOpen tooltip property, but according to this book you are better off using a Popup control (just make it look like a tool tip).

Here is a quote from the book:

Has no effect in practice. The intended purpose of this property is to allow you to create a tooltip that remains open until the user clicks somewhere else. However, the ToolTipService.ShowDuration property overrides the StaysOpen property. As a result, tooltips always disappear after a configurable amount of time (usually about 5 seconds) or when the user moves the mouse away. If you want to create a tooltip-like window that stays open indefinitely, the easiest approach is to use the Popup control.

like image 164
Jason Down Avatar answered Oct 04 '22 03:10

Jason Down


The simplest way is to use Popup. Look to the code sample.

<!--Your ToolTip-->
<Popup x:Name="InfoPopup" PlacementTarget="{Binding ElementName=yourElement}" AllowsTransparency="True" StaysOpen="False" Placement="Mouse" PopupAnimation="Fade">
    <Border BorderBrush="White" BorderThickness="1" Background="#FFFFFFFF" >
        <Label Content="Your text here" />
    </Border>
</Popup>

<!--Your element. Border, Button etc..-->
<Border x:Name="yourElement" Background="#FFFFFF" MinWidth="20" Height="20">
    <Border.Triggers>
        <EventTrigger RoutedEvent="Mouse.MouseDown">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Duration="0:0:0:0" Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="InfoPopup">
                        <DiscreteBooleanKeyFrame Value="True"></DiscreteBooleanKeyFrame>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>

        </EventTrigger>
        <EventTrigger RoutedEvent="Mouse.MouseUp">
            <BeginStoryboard>
                <Storyboard>
                    <BooleanAnimationUsingKeyFrames Duration="0:0:0:0" Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="InfoPopup">
                        <DiscreteBooleanKeyFrame Value="False"></DiscreteBooleanKeyFrame>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Border.Triggers>
</Border>
like image 41
supertoha Avatar answered Oct 04 '22 03:10

supertoha