0

I have an WPF Popup which I apply to a WPF Label:

<Style x:Key="myPopupStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                  <!-- Here the data template -->
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Label x:Name="myLabel"
               MouseEnter="myLabel_ShowTooltip"
               MouseLeave="myLabel_CloseTooltip">

<Popup x:Name="tooltipPopupMyLabel"
               AllowsTransparency="True"
               StaysOpen="False"
               UseLayoutRounding="True"
               PlacementTarget="{Binding ElementName=myLabel}"
               Placement="Bottom">
        <ContentControl Content="this is a tooltip popup!" Style="{StaticResource myPopupStyle}"/>
</Popup>

And the event handlers below:

    private void myLabel_ShowTooltip(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (!this.tooltipPopupMyLabel.IsOpen)
        {
            this.tooltipPopupMyLabel.IsOpen = true;
            // If I uncomment below line of code it stops flickering.
            //this.tooltipPopupMyLabel.StaysOpen = true;
        }
    }

    private void myLabel_CloseTooltip(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (this.tooltipPopupMyLabel.IsOpen)
        {
            this.tooltipPopupMyLabel.IsOpen = false;
            //this.tooltipPopupMyLabel.StaysOpen = false;
        }
    }

When I hold on the mouse on the label, the tooltip popup is shown fine but if I keep the mouse on it without moving it for some time, I can see that it is producing flickering, it looks like popup is being closed and reopened quickly.

If in the myLabel_ShowTooltip I uncomment the line this.tooltipPopupMyLabel.StaysOpen = true; then no flickering is produced. Why I get this flickering if I don't set StaysOpen=true?

ASh
  • 34,632
  • 9
  • 60
  • 82
Willy
  • 9,848
  • 22
  • 141
  • 284
  • 1
    "*I can see that it is producing flickering, it looks like popup is being closed and reopened quickly*" - which does not happen when you set `StaysOpen = true`. Sounds perfectly logical. If you want to avoid the flickering, set StaysOpen. – Clemens Nov 29 '22 at 17:04

1 Answers1

1

put Label and Popup in the same container and use container's mouse events:

<Grid Background="Transparent"
      MouseEnter="myLabel_ShowTooltip"
      MouseLeave="myLabel_CloseTooltip">
    <Label x:Name="myLabel"/>
    
    <Popup x:Name="tooltipPopupMyLabel"
                   AllowsTransparency="True"
                   StaysOpen="True"
                   UseLayoutRounding="True"
                   PlacementTarget="{Binding ElementName=myLabel}"
                   Placement="Bottom">
            <ContentControl Content="this is a tooltip popup!" Style="{StaticResource myPopupStyle}"/>
    </Popup>
</Grid>

you can also try discrete animations instead of event handlers, like shown in this Q&A

ASh
  • 34,632
  • 9
  • 60
  • 82